失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【LightOJ - 1031】Easy Game (区间dp 博弈)

【LightOJ - 1031】Easy Game (区间dp 博弈)

时间:2023-08-26 19:08:13

相关推荐

【LightOJ - 1031】Easy Game (区间dp 博弈)

题干:

You are playing a two player game. Initially there areninteger numbers in an array and playerAandBget chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and playerAstarts the game then how much more point can playerAget than playerB?

Input

Input starts with an integerT (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integerN (1 ≤ N ≤ 100)denoting the size of the array. The next line containsNspace separated integers. You may assume that no number will contain more than4digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input

2

4

4 -10 -20 7

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

解题报告:

做过的原题【UVA - 10891 Game of Sum 】【HRBUST - 1622】 Alice and Bob 不解释了、、

AC代码:

#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<map>#include<vector>#include<set>#include<string>#include<cmath>#include<cstring>#define ll long long#define pb push_back#define pm make_pair#define fi first#define se secondusing namespace std;const int MAX = 2e5 + 5;const int INF = 0x3f3f3f3f;ll dp[205][205];ll sum[205],a[205];ll dfs(int l,int r) {if(dp[l][r] != -1) return dp[l][r];if(l == r) return dp[l][l]=a[l];ll maxx = -INF;for(int i = l+1; i<=r; i++) {maxx = max(maxx , sum[r] - sum[l-1] - dfs(i,r));}for(int i = r-1; i>=l; i--) {maxx = max(maxx , sum[r] - sum[l-1] - dfs(l,i));}maxx = max(maxx,sum[r] - sum[l-1]);return dp[l][r] = maxx;}int main(){int t,n;cin>>t;int iCase = 0;while(t--) {scanf("%d",&n);memset(sum,0,sizeof sum);memset(dp,-1,sizeof dp);for(int i = 1; i<=n; i++) scanf("%lld",&a[i]),sum[i] = sum[i-1] + a[i];printf("Case %d: %lld\n",++iCase,2*dfs(1,n) - sum[n]);}return 0 ;}

如果觉得《【LightOJ - 1031】Easy Game (区间dp 博弈)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。