国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乡族自治县| 新巴尔虎左旗| 涞水县| 十堰市| 济阳县| 赤水市| 贡觉县| 神农架林区| 易门县| 宁津县| 孟连| 嘉定区| 碌曲县| 六盘水市| 信丰县| 韩城市| 宜君县| 类乌齐县| 乡城县| 宣汉县| 淮滨县| 遂昌县| 图木舒克市| 渭源县| 新邵县| 贵定县| 海淀区| 烟台市| 天等县| 大兴区| 岚皋县| 都江堰市| 鲁甸县| 彭州市| 聂拉木县| 铅山县| 林芝县| 邮箱| 丽水市| 定襄县| 宣城市|