LightOJ 1031-Easy Game

来源:互联网 发布:淘宝网电器城 编辑:程序博客网 时间:2024/04/30 11:57

题意:

给你一个数列,每次可以从左边或者右边去一串连续的数字,并且获得所有数字和的分数,两个人轮流取,A先手,B后手,问最后A最多能比B多多少分。很明显的区间dp,但是又有一些博弈在里面。dp[i][j]表示从ij,能多拿的分数。转移方程是这样的:    for (int i=l;i<=r;i++) ans=max(ans,sum[i]-sum[l-1]-dp[i=1][r]);    for (int i=r;i>=l;i--) ans=max(ans,sum[r]-sum[i-1]-dp[l,i-1]);    为什么这样是正确的,我们来看,在当前状态下,我们有两个选择,从左边那一串或者从右边拿一串,假设现在是A来拿,那么拿了之后,暂且比B多了一段连续的分数,但是接下来又会轮到B拿,在回到这个状态之后,B会比A多拿一些,然后就用A暂时多拿的减去B比A多拿的,就是A比B多拿的分数。也许有点绕,多想想就懂了。具体看代码。

代码:

////  Created by  CQU_CST_WuErli//  Copyright (c) 2015 CQU_CST_WuErli. All rights reserved.//// #include<bits/stdc++.h>#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define ALL(x) x.begin(),x.end()#define AT(i,v) for (auto &i:v)#define For_UVa if (kase!=1) cout << endl#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define look(x) cout << #x << "=" << x#define SI(a) scanf("%d",&a)#define SII(a,b) scanf("%d%d",&a,&b)#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Root 1,n,1#define BigInteger bigntemplate <typename T> T max(T& a,T& b) {return a>b?a:b;}template <typename T> T min(T& a,T& b) {return a<b?a:b;}int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}long long gcd (long long a,long long b) {return b==0LL?a:gcd(b,a%b);}const int MAX_L=2005;// For BigIntegerconst int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7fffffff;const int MOD=1e9+7;const double eps=1e-9;const double pi=acos(-1);typedef long long  ll;using namespace std;const int N=110;int dp[N][N];int n;int a[N];int sum[N];int dfs(int l,int r) {    if (l==r) return a[l];    if (l>r) return 0;    int &ans=dp[l][r];    if (ans!=-1) return ans;    ans=-INF_INT;    for (int i=l;i<=r;i++) ans=max(ans,sum[i]-sum[l-1]-dfs(i+1,r));    for (int i=r;i>=l;i--) ans=max(ans,sum[r]-sum[i-1]-dfs(l,i-1));    return ans;}int main(){#ifdef LOCAL    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);//  freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif    int T_T;    for (int kase=scanf("%d",&T_T);kase<=T_T;kase++) {        cin >> n;        CLR(sum);        for (int i=1;i<=n;i++) {            cin >> a[i];            sum[i]=sum[i-1]+a[i];                   }        OFF(dp);        int ans=dfs(1,n);        cout << "Case " << kase << ": " << ans << endl;    }    return 0;}
0 0
原创粉丝点击