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.
有一个式子,空了n个符号,其中p个加号m个减号,要求填充+和-的位置使得式子的运算结果最大。
对于+,要使结果最大,符号两边都要最大。
对于+,要使结果最小,符号两边都要最小。
对于-,要使结果最大,符号左边要最大,右边要最小。
对于-,要使结果最小,符号左边要最小,右边要最大。
由此从括号的最里层一层层向外DP。
注意到虽然符号总数很多,但min(p,m)<=100,可以利用这点压缩状态总数。
定义dp[i][j][0]表示在第i个()里面,使用了j个总数较少的符号,运算结果的最大值。
定义dp[i][j][1]表示在第i个()里面,使用了j个总数较少的符号,运算结果的最小值。
则dfs算好这层()里面的?两边的dp值之后,可以很容易的根据之前总结的+、-运算性质写出DP公式。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=10005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char s[maxn];
int a[maxn],p[maxn];
ll dp[maxn][105][2];
int num=0,x,y;
int dfs(int now,ll l,ll r) {
if (l==r) {
dp[now][0][0]=dp[now][0][1]=s[l]-'0';
return 0;
}
int lc,rc,ls,rs;
lc=++num;
ls=dfs(num,l+1,p[l]-1);
rc=++num;
rs=dfs(num,p[l]+1,r-1);
if (x<y)
for (int i=0;i<=min(x,ls+rs+1);i++) {
for (int j=0;j<=min(ls,i);j++) {
if (j!=i)
dp[now][i][0]=max(dp[now][i][0],dp[lc][j][0]+dp[rc][i-j-1][0]);
dp[now][i][0]=max(dp[now][i][0],dp[lc][j][0]-dp[rc][i-j][1]);
if (j!=i)
dp[now][i][1]=min(dp[now][i][1],dp[lc][j][1]+dp[rc][i-j-1][1]);
dp[now][i][1]=min(dp[now][i][1],dp[lc][j][1]-dp[rc][i-j][0]);
}
}
else
for (int i=0;i<=min(y,ls+rs+1);i++) {
for (int j=0;j<=min(ls,i);j++) {
dp[now][i][0]=max(dp[now][i][0],dp[lc][j][0]+dp[rc][i-j][0]);
if (j!=i)
dp[now][i][0]=max(dp[now][i][0],dp[lc][j][0]-dp[rc][i-j-1][1]);
dp[now][i][1]=min(dp[now][i][1],dp[lc][j][1]+dp[rc][i-j][1]);
if (j!=i)
dp[now][i][1]=min(dp[now][i][1],dp[lc][j][1]-dp[rc][i-j-1][0]);
}
}
return ls+rs+1;
}
int main() {
scanf("%s",s+1);
int len,i,j;
len=strlen(s+1);
if (len==1) {
printf("%s",s+1);return 0;
}
scanf("%d%d",&x,&y);
stack<int> st;
for (i=1;i<=len;i++) {
if (s[i]=='(') st.push(i);
if (s[i]==')') {
a[st.top()]=i;
st.pop();
}
if (s[i]=='?') p[st.top()]=i;
}
num=1;
for (i=1;i<=len;i++) {
for (j=0;j<=min(x,y);j++) {
dp[i][j][0]=-inf;dp[i][j][1]=inf;
}
}
dfs(1,1,len);
printf("%I64d\n",dp[1][min(x,y)][0]);
return 0;
}