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

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84744894

题干:

You are playing a two player game. Initially there are n integer numbers in an array and player A and B get 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 player A starts the game then how much more point can player A get than player B?

Input

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

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

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 second
using 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 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84744894