树型dp也是练得少啊 。。弄了半天才好。。
这个就是把数字看成叶子,把?看出非叶子结点就行了。。。
然后其实+-号的个数只看其中一个,另外一个就已经知道了,不用理会。。
设d[i][j]、f[i][j]为使用了j个+号,i结点为根子树中的表达式的最大值和最小值
然后分别转移就可以。。注意无论最大还是最小,+和-都要尝试。。举一些全为负数的例子应该就能明白。。
最后坑爹的地方就是。。把min(m,p)<100看成了max(m,p)<100。。。所以会出现+号贼多的情况。。要先预判一下看+还是看-。。。
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 20005
#define nm 105
#define pi 3.1415926535897931
using namespace std;
const ll inf=1000000007;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int root,d[NM][nm],f[NM][nm],l[NM],r[NM],m,tot,tmp,n,_f=1;
char s[NM];
void dfs(int &x,int &i){
i=++tot;
inc(j,0,n)d[i][j]=-inf,f[i][j]=inf;
if(isdigit(s[x])){d[i][0]=f[i][0]=s[x]-'0';return;}
dfs(++x,l[i]);
dfs(x+=2,r[i]);
x++;
inc(j,0,n)inc(k,0,j-(_f^1))
f[i][j]=min(f[i][j],f[l[i]][k]-d[r[i]][j-k-(_f^1)]),
d[i][j]=max(d[i][j],d[l[i]][k]-f[r[i]][j-k-(_f^1)]);
inc(j,0,n)inc(k,0,j-(_f^0))
d[i][j]=max(d[i][j],d[l[i]][k]+d[r[i]][j-k-(_f^0)]),
f[i][j]=min(f[i][j],f[l[i]][k]+f[r[i]][j-k-(_f^0)]);
}
int main(){
//freopen("data.in","r",stdin);
scanf("%s",s+1);
m=strlen(s);
n=read();tmp=read();
if(tmp<n){swap(tmp,n);_f=0;}
tmp=1;
dfs(tmp,root);
printf("%d\n",d[root][n]);
return 0;
}
Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression.
An Ahmes arithmetic expression can be defined as:
- "d" is an Ahmes arithmetic expression, where d is a one-digit positive integer;
- "(E1 op E2)" is an Ahmes arithmetic expression, where E1 and E2 are valid Ahmes arithmetic expressions (without spaces) and op is either plus ( + ) or minus ( - ).
On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task:
Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators.
The first line contains a string E (1 ≤ |E| ≤ 104) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'.
The second line contains two space-separated integers P and M (0 ≤ min(P, M) ≤ 100) — the number of plus and minus operators, respectively.
It is guaranteed that P + M = the number of erased operators.
Print one line containing the answer to the problem.
(1?1) 1 0
2
(2?(1?2)) 1 1
1
((1?(5?7))?((6?2)?7)) 3 2
18
((1?(5?7))?((6?2)?7)) 2 3
16
- The first sample will be (1 + 1) = 2.
- The second sample will be (2 + (1 - 2)) = 1.
- The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7)) = 18.
- The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7)) = 16.