Codeforces 935E Fafa and Ancient Mathematics dp
转换成树上问题dp一下。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e4 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); char s[N];
int P, M, n, ans;
int mxdp[N][], mndp[N][], cnt[N];
int weight[N]; inline bool chkmax(int &a, int b) {
return a < b ? a = b, true : false;
}
inline bool chkmin(int &a, int b) {
return a > b ? a = b, true : false;
} int getPos(int l, int r) {
int w = , pos = -;
int digit = -;
for(int i = l; i <= r; i++) {
if(s[i] == '(') w += ;
else if(s[i] == ')') w -= ;
else if(s[i] == '?') weight[i] = w;
if(isdigit(s[i])) digit = i;
}
for(int i = l; i <= r; i++) {
if(s[i] == '?') {
if(pos == - || weight[pos] > weight[i]) pos = i;
}
}
if(~pos) return pos;
return digit;
} void dfs(int l, int r, int rt) {
if(s[rt] != '?') {
mxdp[rt][] = s[rt] - '';
mndp[rt][] = s[rt] - '';
return;
}
int ls = getPos(l, rt - );
int rs = getPos(rt + , r);
dfs(l, rt - , ls);
dfs(rt + , r, rs);
cnt[rt] = cnt[ls] + cnt[rs] + ;
if(P < M) {
for(int i = ; i <= cnt[ls]; i++) {
for(int j = ; j <= cnt[rs]; j++) {
if(i + j > P) continue;
if(P - i - j > ) {
chkmax(mxdp[rt][i + j + ], mxdp[ls][i] + mxdp[rs][j]);
chkmin(mndp[rt][i + j + ], mndp[ls][i] + mndp[rs][j]);
}
if(M - (cnt[ls] - i) - (cnt[rs] - j) > ) {
chkmax(mxdp[rt][i + j], mxdp[ls][i] - mndp[rs][j]);
chkmin(mndp[rt][i + j], mndp[ls][i] - mxdp[rs][j]);
}
}
}
} else {
for(int i = ; i <= cnt[ls]; i++) {
for(int j = ; j <= cnt[rs]; j++) {
if(i + j > M) continue;
if(M - i - j > ) {
chkmax(mxdp[rt][i + j + ], mxdp[ls][i] - mndp[rs][j]);
chkmin(mndp[rt][i + j + ], mndp[ls][i] - mxdp[rs][j]);
}
if(P - (cnt[ls] - i) - (cnt[rs] - j) > ) {
chkmax(mxdp[rt][i + j], mxdp[ls][i] + mxdp[rs][j]);
chkmin(mndp[rt][i + j], mndp[ls][i] + mndp[rs][j]);
}
}
}
}
} int main() {
scanf("%s", s + );
n = strlen(s + );
scanf("%d%d", &P, &M);
for(int i = ; i <= n; i++)
for(int j = ; j <= min(P, M); j++)
mxdp[i][j] = -inf, mndp[i][j] = inf;
int root = getPos(, n);
dfs(, n, root);
printf("%d\n", mxdp[root][min(P, M)]);
return ;
} /*
*/
Codeforces 935E Fafa and Ancient Mathematics dp的更多相关文章
- Codeforces 935E Fafa and Ancient Mathematics(表达式转树 + 树型DP)
题目链接 Codeforces Round #465 (Div. 2) Problem E 题意 给定一个表达式,然后用$P$个加号和$M$个减号填充所有的问号(保证问号个数等于$P + M$) ...
- CodeForces 935E Fafa and Ancient Mathematics (树形DP)
题意:给定一个表达式,然后让你添加 n 个加号,m 个减号,使得表达式的值最大. 析:首先先要建立一个表达式树,这个应该很好建立,就不说了,dp[u][i][0] 表示 u 这个部分表达式,添加 i ...
- 2018.12.12 codeforces 935D. Fafa and Ancient Alphabet(概率dp)
传送门 概率dp水题. 题意简述:给你数字表的大小和两个数列,数列中为0的数表示不确定,不为0的表示确定的,求第一个数列字典序比第二个数列大的概率. fif_ifi表示第i ni~ ni n位第一个 ...
- 【学术篇】CF935E Fafa and Ancient Mathematics 树形dp
前言 这是一道cf的比赛题.. 比赛的时候C题因为自己加了一个很显然不对的特判WA了7次但找不出原因就弃疗了... 然后就想划水, 但是只做了AB又不太好... 估计rating会掉惨 (然而事实证明 ...
- Codeforces 935D Fafa and Ancient Alphabet
题目链接 题意 给定两个\(n\)位的\(m\)进制数\(s1,s2\),所有出现的\(0\)均可等概率地被其他数字替换,求\(s1\gt s2\)的概率. 思路 从高位到低位,根据每一位上相应的\( ...
- [Codeforces 865C]Gotta Go Fast(期望dp+二分答案)
[Codeforces 865C]Gotta Go Fast(期望dp+二分答案) 题面 一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每 ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- [Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT)
[Codeforces 553E]Kyoya and Train(期望DP+Floyd+分治FFT) 题面 给出一个\(n\)个点\(m\)条边的有向图(可能有环),走每条边需要支付一个价格\(c_i ...
- Codeforces 611D New Year and Ancient Prophecy dp+字符串比较
这是CF Goodbye 2015 的D题,当时我想了一个n^3的dp算法,肯定不能过,然后听到学长后缀数组的n^2log(n)写法,仰慕 最后打完比赛看到了t神的n^2写法,简直膜拜,直接省去了后缀 ...
随机推荐
- 前端跨域问题的总结&&nodejs 中间层的路由转发
前后端交互的时候,跨域是避不开的问题. 总结就是如下: 1.Cors 我在做前后端分离的时候,会采用cors 的方法:便于其他源的调用接口,这个可以设置成任意的源头,也可以允许指定的源头. 下面的是n ...
- 51nod--1264 线段相交 (计算几何基础, 二维)
题目: 1264 线段相交 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为 ...
- python学习第38天
mysql的存储引擎(innodb,myisam)mysql支持的数据类型约束表的创建\删除\修改\查看表结构表与表之间的关系
- 行为驱动:BDD框架之Cucumber初探
1.cucumber cucumber早在ruby环境下应用广泛,作为BDD框架的先驱,cucumber后来被移植到了多平台,简单来说cucumber是一个测试框架,就像是juint或是rspec一样 ...
- 安装snap及snap常安装软件
文章链接:https://blog.csdn.net/laomd/article/details/80710451 一.snap简介 什么是snap,snap是一种全新的软件包管理方式,它类似一个容器 ...
- Android gradle provided、implementation等指令注意点
其实这类文章博客网上一搜一大堆,但有些地方可能说的不太清楚(都一样的内容,抄袭太严重),这里只是做个精简的总结和一些其他地方没提到的点. 一.Android Studio 3.0开始使用了新的指令,原 ...
- 经典JS闭包面试题(来理解闭包)(转)
转载地址:http://www.cnblogs.com/xxcanghai/p/4991870.html 先看代码: function fun(n,o) { console.log(o) return ...
- L1和L2正则
https://blog.csdn.net/jinping_shi/article/details/52433975
- Java中关于string的些许问题及解析
问题一:String 和 StringBuffer 的区别JAVA 平台提供了两个类: String 和 StringBuf fer ,它们可以储存和操作字符串,即包含多个字符的字符数据.这个 Str ...
- Vue.extend和Vue.component的联系与差异
extend 是构造一个组件的语法器. 你给它参数 他给你一个组件 然后这个组件 你可以作用到Vue.component 这个全局注册方法里, 也可以在任意vue模板里使用apple组件 var ap ...