首页 > 学院 > 开发设计 > 正文

UVa 10891 Game of Sum

2019-11-06 06:20:11
字体:
来源:转载
供稿:网友
This is a two player game. Initially there are n integer numbers in an array and players A and B getchance to take them alternatively. Each player can take one or more numbers from the left or right endof the array but cannot take from both ends at a time. He can take as many consecutive numbers ashe 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. Eachplayer tries to achieve more points from other. If both players play optimally and player A starts thegame then how much more point can player A get than player B?InputThe input consists of a number of cases. Each case starts with a line specifying the integer n (0 <n ≤ 100), the number of elements in the array. After that, n numbers are given for the game. Input isterminated by a line where n = 0.OutputFor each test case, PRint a number, which represents the maximum difference that the first playerobtained after playing this game optimally.Sample Input44 -10 -20 741 2 3 40Sample Output7

10

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

区间DP+博弈论~

看起来是博弈论的题目,实际上却是DP~

用f[i][j]表示剩下的区间是[i,j]时从一边取数的最大值,那么枚举中间值k,f[i][j]=min(f[i][j],sum[i][j]-min(f[i][k],f[k+1][j])),其中sum[i][j]表示区间[i,j]的数值总和。可以由前缀和求出。

要注意的是[i,j]此时取数的人也可以直接取走所有数,所以f[i][j]的初始值为sum[i][j]~

最后输出a-b,注意到a+b=sum[1][n],那么答案就是f[1][n]-(sum[1][n]-f[1][n]),化简得f[1][n]*2-sum[1][n]~

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,a[101],f[101][101],inf;int read(){	int totnum=0,f=1;char ch=getchar();	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}	while(ch>='0' && ch<='9') {totnum=(totnum<<1)+(totnum<<3)+ch-'0';ch=getchar();}	return totnum*f;}int main(){	while(1)	{		n=read();		if(!n) break;		for(int i=1;i<=n;i++) f[i][i]=read(),a[i]=a[i-1]+f[i][i];		for(int l=2;l<=n;l++)		  for(int i=1,j=l;j<=n;i++,j++)		  {		  	f[i][j]=a[j]-a[i-1];		  	for(int k=i;k<j;k++) f[i][j]=max(f[i][j],a[j]-a[i-1]-min(f[i][k],f[k+1][j]));		  }		printf("%d/n",f[1][n]*2-a[n]);	}	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表