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写法,简直膜拜,直接省去了后缀 ...
随机推荐
- Redis持久化存储(AOF与RDB两种模式)
Redis中数据存储模式有2种:cache-only,persistence; cache-only即只做为“缓存”服务,不持久数据,数据在服务终止后将消失,此模式下也将不存在“数据恢复”的手段,是一 ...
- ASP.NET MVC5高级编程 之 HTML辅助方法
Html属性调用HTML辅助方法,Url属性调用URL辅助方法,Ajax属性调用Ajax辅助方法. HTML辅助方法 1.Html.BeginForm @using (Html.BeginForm(& ...
- java结合testng,利用txt做数据源的数据驱动实例
import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.Buffe ...
- 2018 github热门项目
github流行的几个项目,我们来学习一下. 1. developer-roadmap-chinese image.png 项目简介:2018年web程序员路线中文版, 这个仓库里包含了一些前端,后端 ...
- iOS 在 程序内调用手机上安装的地图软件进行导航
// 需求是需要用户 能从 所在位置 到 附近的健身房的 路线, 然而,就一个需求,不值当的添加一个地图, 就用调用手机上第三方地图软件, 什么高德, 百度, 腾讯, iOS 原生地图都可以, 如果 ...
- Java Web 开发的JavaBean + Servlet + Sql Server
日期:2018.12.9 博客期:026 星期日 我知道对于每个人都需要对开发web进行了解,而我们常用的技术,也应该有所了解 /*<------------------->*/知识点: ...
- vuex action 与mutations 的区别
面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...
- Linux 用户(user)和用户组(group)管理概述
一.理解Linux的单用户多任务,多用户多任务概念: Linux 是一个多用户.多任务的操作系统:我们应该了解单用户多任务和多用户多任务的概念: 1.Linux 的单用户多任务:单用户多任务:比如我们 ...
- laravel 更新
public function update(Request $request, ResponseFactoryContract $response) { $user = $request->u ...
- react 使用draft.js富文本编辑器
参照网址:https://www.cnblogs.com/3body/p/6224010.html 参看网址:https://www.cnblogs.com/mosquito18/p/9787816. ...