题目链接  Codeforces Round #465 (Div. 2) Problem E

题意  给定一个表达式,然后用$P$个加号和$M$个减号填充所有的问号(保证问号个数等于$P + M$)

   求可以形成的表达式的最大值。

先把表达式转成一棵树,然后在树上DP。

题目保证了$min(P, M) <= 100$, 为了提高效率,我们选择用少的运算符号作为DP的第二维。

对$P$和$M$的大小关系进行分类讨论。

当$P < M$时,

设$f[i][j]$表示$i$代表的子树里面填$j$个加号能得到的结果的最大值。

$c[i][j]$表示$i$代表的子树里面填$j$个减号能得到的结果的最小值。

那么转移就是

$f[x][i+j+1] = max(f[x][i+j+1], f[l][i] + l[r][j])$

$f[x][i+j] = max(f[x][i+j], f[l][i] - c[r][j])$

$c[x][i+j+1] = min(c[x][i+j+1], c[l][i] + c[r][j])$

$c[x][i+j] = min(c[x][i+j], c[l][i] - f[r][j])$

当$P > M$时,

设$f[i][j]$表示$i$代表的子树里面填$j$个加号能得到的结果的最大值。

$c[i][j]$表示$i$代表的子树里面填$j$个减号能得到的结果的最小值。

设个时候转移方程为

$f[x][i+j] = max(f[x][i+j], f[l][i] + l[r][j])$

$f[x][i+j+1] = max(f[x][i+j+1], f[l][i] - c[r][j])$

$c[x][i+j] = min(c[x][i+j], c[l][i] + c[r][j])$

$c[x][i+j+1] = min(c[x][i+j+1], c[l][i] - f[r][j])$

程序里把两种情况合起来写了,看起来更加简洁。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 1e4 + 10;
const int M = 105;
const int inf = 1e9; char st[N];
int val[N];
int p, m, n;
int f[N][M], c[N][M];
vector <int> v[N];
stack <int> stk; inline void upmax(int &a, int b){ a = max(a, b);}
inline void upmin(int &a, int b){ a = min(a, b);} void dfs(int x, int t){
rep(i, 0, M - 1) f[x][i] = -inf, c[x][i] = inf;
if (val[x]){
f[x][0] = c[x][0] = val[x];
return;
} int l = v[x][0], r = v[x][1];
dfs(l, t), dfs(r, t); rep(i, 0, M - 1) if (f[l][i] > -inf){
rep(j, 0, M - 1) if (f[r][j] > -inf){
upmax(f[x][i + j + t], f[l][i] + f[r][j]);
upmax(f[x][i + j + (t ^ 1)], f[l][i] - c[r][j]);
upmin(c[x][i + j + t], c[l][i] + c[r][j]);
upmin(c[x][i + j + (t ^ 1)], c[l][i] - f[r][j]);
}
}
} int main(){ scanf("%s%d%d", st, &p, &m); for (int i = 0; st[i]; ++i){
if (st[i] == '(') stk.push(++n);
else if (st[i] == ')'){
int t = stk.top();
stk.pop();
if (!stk.empty()) v[stk.top()].push_back(t);
} else if (st[i] >= '1' && st[i] <= '9'){
if (stk.empty()) return 0 * printf("%d\n", st[i] - '0');
v[stk.top()].push_back(++n);
val[n] = st[i] - '0';
}
} dfs(1, p < m);
printf("%d\n", f[1][min(p, m)]);
return 0;
}

  

Codeforces 935E Fafa and Ancient Mathematics(表达式转树 + 树型DP)的更多相关文章

  1. Codeforces 935E Fafa and Ancient Mathematics dp

    Fafa and Ancient Mathematics 转换成树上问题dp一下. #include<bits/stdc++.h> #define LL long long #define ...

  2. CodeForces 935E Fafa and Ancient Mathematics (树形DP)

    题意:给定一个表达式,然后让你添加 n 个加号,m 个减号,使得表达式的值最大. 析:首先先要建立一个表达式树,这个应该很好建立,就不说了,dp[u][i][0] 表示 u 这个部分表达式,添加 i ...

  3. 【学术篇】CF935E Fafa and Ancient Mathematics 树形dp

    前言 这是一道cf的比赛题.. 比赛的时候C题因为自己加了一个很显然不对的特判WA了7次但找不出原因就弃疗了... 然后就想划水, 但是只做了AB又不太好... 估计rating会掉惨 (然而事实证明 ...

  4. 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)

    传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi​表示第i ni~ ni n位第一个 ...

  5. Codeforces 935D Fafa and Ancient Alphabet

    题目链接 题意 给定两个\(n\)位的\(m\)进制数\(s1,s2\),所有出现的\(0\)均可等概率地被其他数字替换,求\(s1\gt s2\)的概率. 思路 从高位到低位,根据每一位上相应的\( ...

  6. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  7. Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp

    D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...

  8. Codeforces 552E - Vanya and Brackets【表达式求值】

    给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...

  9. codeforces 552 E. Vanya and Brackets 表达式求值

    题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...

随机推荐

  1. SpringMVC 整合 kaptcha(验证码功能)

    一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...

  2. 关于ADB push 出现failed to copy 'D:\file.xtxt' to '/system/temp/' : Read-only file system 的报错信息解决办法

    首先使用USB连接电脑与小机,然后安装adb相应的驱动,这是第一步,也是必须要做的. 进入doc系统后,敲入adb shell  可以进入linux命令行状态,说明adb可以使用了. 回到标题,我们现 ...

  3. (转) Unreal的HLSL交叉编译-UEAPI

    HLSL Cross Compiler This library compiles High Level Shading Language (HLSL) shader source code into ...

  4. Where can I find the IPA logs

    Retrieving the IPA logs will differ depending on which base image was used. Operating system that do ...

  5. 线段树(单点更新,区间查询) HDU 1754 I Hate It

    题目链接 线段树的模板 #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  6. svn 数据库 账号密码

    47.106.107.201 云商传媒李孔文 2018/8/22 19:26:35 root 云商传媒李孔文 2018/8/22 19:26:39 sn123456 云商传媒李孔文 2018/8/22 ...

  7. 贪吃蛇StringBuilder 和 定时器

    ConsoleKeyInfo info = Console.ReadKey(); while (true) { if (info.Key == ConsoleKey.UpArrow)//只能输入一次但 ...

  8. ThreadPoolExecutor源码解析

    LZ目前正在做一个批量生成报表的系统,需要定时批量生成多张报表,便考虑使用线程池来完成.JDK自带的Executors工具类只提供创建固定线程和可伸展但无上限的两个静态方法,并不能满足LZ想自定制线程 ...

  9. 【bzoj4378】[POI2015]Logistyka 离散化+树状数组

    题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作.每次 ...

  10. windows安装 Nodejs,NPM, CNPM

    1, dowload Nodejs from :  http://nodejs.cn/download/ 下载完毕后,可以安装node,建议不要安装在系统盘(本例安装在 D:\vueProject) ...