国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝南县| 眉山市| 游戏| 南华县| 衡阳市| 丁青县| 大关县| 古田县| 新丰县| 莒南县| 松桃| 积石山| 云南省| 隆德县| 望江县| 郸城县| 保康县| 徐州市| 财经| 兴安县| 松原市| 元朗区| 孟津县| 呼图壁县| 宜宾县| 南部县| 伊宁市| 苏州市| 灵石县| 平谷区| 呼伦贝尔市| 格尔木市| 玉门市| 伊金霍洛旗| 紫金县| 太原市| 磐安县| 商南县| 门源| 贵定县| 玉环县|