Codeforces 935E Fafa and Ancient Mathematics DP

E. Fafa and Ancient Mathematics
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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;
  • "(E1op E2)" is an Ahmes arithmetic expression, where E1 and E2 are valid Ahmes arithmetic expressions (without spaces) and op is either plus ( + ) or minus ( - ).
For example  5(1-1) and  ((1+(2-3))-5) are valid Ahmes arithmetic expressions.

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.

Input

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.

Output

Print one line containing the answer to the problem.

Examples
input
Copy
(1?1)
1 0
output
2
input
Copy
(2?(1?2))
1 1
output
1
input
Copy
((1?(5?7))?((6?2)?7))
3 2
output
18
input
Copy
((1?(5?7))?((6?2)?7))
2 3
output
16
Note
  • 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
区间DP是一种动态规划的方法,用于解决区间范围内的问题。在Codeforces竞赛中,区间DP经常被用于解决一些复杂的字符串或序列相关的问题。 在区间DP中,dp[i][j]表示第一个序列前i个元素和第二个序列前j个元素的最优解。具体的转移方程会根据具体的问题而变化,但是通常会涉及到比较两个序列的元素是否相等,然后根据不同的情况进行状态转移。 对于区间长度为1的情况,可以先进行初始化,然后再通过枚举区间长度和区间左端点,计算出dp[i][j]的值。 以下是一个示例代码,展示了如何使用区间DP来解决一个字符串匹配的问题: #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> using namespace std; const int maxn=510; const int inf=0x3f3f3f3f; int n,dp[maxn][maxn]; char s[maxn]; int main() { scanf("%d", &n); scanf("%s", s + 1); for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int i = 1; i <= n; i++) { if(s[i] == s[i - 1]) dp[i][i - 1] = 1; else dp[i][i - 1] = 2; } for(int len = 3; len <= n; len++) { int r; for(int l = 1; l + len - 1 <= n; l++) { r = l + len - 1; dp[l][r] = inf; if(s[l] == s[r]) dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]); else { for(int k = l; k <= r; k++) { dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]); } } } } printf("%d\n", dp[n]); return 0; } 希望这个例子能帮助你理解区间DP的基本思想和应用方法。如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值