问题来源:刘汝佳《算法竞赛入门经典--训练指南》 P67 例题28:

问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端都取,所有数都被取完时游戏结束,然后统计每个人取走的所有数字之和作为得分,两人的策略都是使自己的得分尽可能高,并且都足够聪明,求A的得分减去B的得分的结果。

问题分析:1.设dp[i][j]表示从第i第j的数的序列中,双方都采取最优策略的前提下,先手得分的最大值

       2.若求dp[i][j],我们可以枚举从左边(或者右边)取多少个数,并求枚举过程中dp[i][j]的最大值,则有状态转移方程

          dp[i][j] = sum[i][j] - Min{dp[i+1][j],dp[i+2][j],...,dp[j][j],  dp[i][i],dp[i][i+1],...,dp[i][j-1]}

      (其中sum[i][j] 表示从i到j的序列和)

例题链接:...

例题:UVa 10891

10891 - Game of Sum

Time limit: 3.000 seconds

  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

O(n3)代码:

 #include "stdio.h"
#include "string.h"
#define N 105
int a[N];
int sum[N];
int dp[N][N],mark[N][N]; int inline Min(int a,int b) { return a<b?a:b; } int DP(int i,int j) //记忆化搜索
{
if(mark[i][j])
return dp[i][j];
mark[i][j] = ;
int m = ; //全部取光
for(int k=i+; k<=j; k++)
m = Min(m,DP(k,j));
for(int k=j-; k>=i; k--)
m = Min(m,DP(i,k));
dp[i][j] = sum[j] - sum[i-] - m;
return dp[i][j];
} int main()
{
int n;
int i;
while(scanf("%d",&n),n!=)
{
for(i=; i<=n; i++)
scanf("%d",&a[i]);
memset(sum,,sizeof(sum));
for(i=; i<=n; i++)
sum[i] = sum[i-] + a[i];
memset(dp,,sizeof(dp));
memset(mark,,sizeof(mark)); //标记初始化
printf("%d\n",*DP(,n)-sum[n]);
}
return ;
}

09_Sum游戏(UVa 10891 Game of Sum)的更多相关文章

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

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

  2. [题解]UVa 10891 Game of Sum

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

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

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

  4. UVA 10891 Game of Sum(区间DP(记忆化搜索))

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

  5. UVA 10891 Game of Sum

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

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

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

  7. UVA - 10891 Game of Sum 区间DP

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

  8. 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 ...

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

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

随机推荐

  1. MEF(Managed Extensibility Framework )的入门介绍

    1.什么是MEF MEF是一个来自于微软协作构建扩展应用的新框架,它的目的是在运行中的应用中添加插件.MEF继承于.NET 4.0 Framework平台,存在于各种应用平台的系统程序集中 2.程序集 ...

  2. Windows Azure开发者任务之五:配置虚拟机的“规模”

    指定虚拟机的“规模”是怎么一回事? 我们可以指定角色将要部署于其上的虚拟机的“规模”.虚拟机的“规模”是指: 1,CPU核心数 2,内存容量 3,本地文件系统的体积 我们可以针对具体的角色来指定虚拟机 ...

  3. PHP实现上传文件并存进数据库的方法

    本文实例讲述了PHP实现上传文件并存进数据库的方法.分享给大家供大家参考.具体如下: show_add.php文件如下: <?php if(!isset($_REQUEST[''id'']) o ...

  4. yyyy/M/d h:m:s 转换成 yyyy-MM-dd hh:mm:ss

    var arrTime = (dtime).replace("/", "-").replace("/", "-"); v ...

  5. 【iOS】Quartz2D简单介绍

    一.什么是Quartz2D Quartz 2D是⼀个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图 ...

  6. 1秒30000QPS,前后端设计思路

    Q:现在有这样一个需求,在一秒中有3万的支付订单请求,有什么比较好的解决方案吗? PS:我们数据库用的是oracle 程序是java spring mybatis dubbo mq等技术,现在有这样一 ...

  7. 推荐两个很好用的javascript模板引擎

    http://www.jsviews.com/#jsrender,支持if/for等常用逻辑,自称下一代jquery template plugin标准 https://github.com/janl ...

  8. Microsoft Dynamics CRM 2013 安装过程 图解

    在安装前,先持一下SQL配置管理,将相关的服务打开.(由于在虚拟机里,许多服务需要时才会打开,像Reporting Services需要处理报表时才打开) 注:Analysis Services 登录 ...

  9. Ieditor

    Interfaces Description IActiveViewEvents (esriCarto) Provides access to events that occur when the s ...

  10. SharePoint 2013 设置自定义布局页

    在SharePoint中,我们经常需要自定义登陆页面.错误页面.拒绝访问等:不知道大家如何操作,以前自己经常在原来页面改或者跳转,其实SharePoint为我们提供了PowerShell命令,来修改这 ...