Problem E
Game of Sum
Input File: e.in

Output: Standard Output

This is a two player game. Initially there are n integer numbers in an array and players 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

The 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 is terminated by a line where n=0.

Output

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

Sample Input                                Output for Sample Input

4

4 -10 -20 7

4

1 2 3 4

0

7

10


Problem setter: Syed Monowar Hossain

Special Thanks: Derek Kisman, Mohammad Sajjad Hossain

整数的总和是一定的,不管怎么取,任意时刻的状态都是原始序列的字串,因此我们想到用d[i][j]表示原始序列i->j个元素组成的子序列,在双方都取得最优策越的情况下先手得到的最高分,状态转移时,我们需要枚举从左边取和从右边取,这等价于给对方剩下怎样的序列。则有 d[i][j]=sum(i,j)-min(d[i+1][j],……d[j][j],d[i][j-1],……d[i][i],0),  则ans=d[1][n]-(sum(1,n)-d[1][n])

 #include"iostream"
#include"cstring"
#include"cstdio"
#include"algorithm"
using namespace std;
const int ms=;
int sum[ms];
int d[ms][ms];
int vis[ms][ms];
int a[ms];
int n;
int dp(int i,int j)
{
if(vis[i][j])
return d[i][j];
int m=; //全部取
vis[i][j]=;
for(int k=i+;k<=j;k++)
m=min(m,dp(k,j));
for(int k=i;k<j;k++)
m=min(m,dp(i,k));
d[i][j]=sum[j]-sum[i-]-m;
return d[i][j];
}
int main()
{
while(scanf("%d",&n)&&n)
{
sum[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
memset(vis,,sizeof(vis));
printf("%d\n",*dp(,n)-sum[n]);
}
return ;
}

10891 - Game of Sum的更多相关文章

  1. 09_Sum游戏(UVa 10891 Game of Sum)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...

  2. uva 10891 Game of Sum(区间dp)

    题目连接:10891 - Game of Sum 题目大意:有n个数字排成一条直线,然后有两个小伙伴来玩游戏, 每个小伙伴每次可以从两端(左或右)中的任意一端取走一个或若干个数(获得价值为取走数之和) ...

  3. [题解]UVa 10891 Game of Sum

    在游戏的任何时刻剩余的都是1 - n中的一个连续子序列.所以可以用dp[i][j]表示在第i个数到第j个数中取数,先手的玩家得到的最大的分值.因为两个人都很聪明,所以等于自己和自己下.基本上每次就都是 ...

  4. UVA 10891 Game of Sum

    题目大意就是有一个整数串,有两个人轮流取,每次可以取走一个前缀或后缀.两人都足够聪明,且都会使自己收益最大.求取完后先手比后手多多少. 每次我看见上面那句就会深感自己的愚笨无知. 所以来推推性质? 1 ...

  5. UVa 10891 Game of Sum - 动态规划

    因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低. 用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分. 转移通过枚举取的数的个数$k$来转移.因为你希望先手得分 ...

  6. UVA - 10891 Game of Sum 区间DP

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461 Game of sum Description This ...

  7. UVA 10891 Game of Sum(DP)

    This is a two player game. Initially there are n integer numbers in an array and players A and B get ...

  8. 28.uva 10891 Game of Sum 记忆化dp

    这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解      请戳这里 ... #include<cstdio> #include<algorithm> #i ...

  9. UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

随机推荐

  1. bzoj 1009 [HNOI2008]GT考试(DP+KMP+矩阵乘法)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1009 [题意] 给定一个字符串T,问长度为n且不包含串T的字符串有多少种. [思路] ...

  2. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  3. c++ 概念及学习/c++ concept&learning(一)

    学习过计算机组成原理就会知道,处理器会从主存中取得指令,然后进行解释执行.而他们的交流方式是以二进制方式进行的,也就是他们只能识别1和0 :其实计算机是不知道1和0的,现在的实现方式是以高电压与低电压 ...

  4. JSON解析关联类型发生死循环 There is a cycle in the hierarchy!

    解决办法是忽略掉关联类型的数据,使用jsonConfig进行配置,代码如下: JsonConfig jsonConfig = new JsonConfig();  //建立配置文件 jsonConfi ...

  5. 视频资源下载方法[download video resources]

    笔者做视频时钟分析,需要用到大量特殊的视频,如何获取需要的视频是一个问题? 以下载NBA视频为例: Tools:①腾讯视频软件 (自行下载)  ②批处理文件(下文会给出代码) convert.bat ...

  6. Crontab设置定时任务

    ​ Crontab文件格式 文件格式: minute hour day month weekday username command 格式解析 字段 描述 minute 分,值为0-59 hour 小 ...

  7. .NET中获取字符串的MD5码

    C# 代码: 导入命名空间(需要在Web页面的代码页中引用) using System.Web.Security; 获取MD5码 string Password = FormsAuthenticati ...

  8. 【转】Maven实战(二)---多模块开发---缺少Jar包

    原博文出于:http://blog.csdn.net/liutengteng130/article/details/41611755    感谢! Maven里面的Jar包经常出现Missing的情况 ...

  9. 【转】Nginx系列(五)--nginx+tomcat实现负载均衡

    原博文出于:  http://blog.csdn.net/liutengteng130/article/details/47129909   感谢! Nginx占有内存少,并发能力强,事实上Nginx ...

  10. Windows下cmd的替代软件——PowerCmd

    Powercmd 是一款运行在windows下的cmd增强软件(A Better Command Prompt Replacement Tool),当前最新的版本为2.2. 官方提供试用版,貌似没有功 ...