UVA - 10891 Game of Sum 区间DP
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461
Game of sum
Description
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 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
4
4 -10 -20 7
4
1 2 3 4
0
Sample Output
7
10
Hint
题意:
给你一个长度n的数组,AB两人轮流开始选一段数,至少一个,但是只能从两端开始,问你最后每个人选的权值和A最多比B多多少,A,B一样聪明
题解:
区间DP,dp[i][j]表示从i,j内最佳答案
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e2+,inf = 2e9, mod = 1e9+;
typedef long long ll; const ll INF = 1e18; ll dp[N][N],a[N],sum[N],n; ll dfs(int l,int r)
{
if(dp[l][r]!=INF)
{
return dp[l][r];
}
ll &ret=dp[l][r];
ret=-INF;
for(int i=l;i<r;i++)
{
ret = max(ret,sum[i]-sum[l-] - dfs(i+,r));
}
for(int i=r;i>l;i--)
{
ret = max(ret,sum[r]-sum[i-] - dfs(l,i-));
}
ret = max(ret,sum[r]-sum[l-]);
return ret;
}
int main()
{
while(scanf("%lld",&n)!=EOF)
{
if(n==)break;
for(int i=;i<=n+;i++)
for(int j=;j<=n+;j++) dp[i][j]=INF;
sum[]=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=(sum[i-]+a[i]);
}
printf("%lld\n",dfs(,n));
}
return ;
}
UVA - 10891 Game of Sum 区间DP的更多相关文章
- uva 10891 Game of Sum(区间dp)
题目连接:10891 - Game of Sum 题目大意:有n个数字排成一条直线,然后有两个小伙伴来玩游戏, 每个小伙伴每次可以从两端(左或右)中的任意一端取走一个或若干个数(获得价值为取走数之和) ...
- 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 ...
- 09_Sum游戏(UVa 10891 Game of Sum)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...
- UVa 10891 - Game of Sum 动态规划,博弈 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- uva 10003 Cutting Sticks 【区间dp】
题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA - 10891 Game of Sum (区间dp)
题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...
- 28.uva 10891 Game of Sum 记忆化dp
这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解 请戳这里 ... #include<cstdio> #include<algorithm> #i ...
- UVa 10891 Game of Sum (DP)
题意:给定一个长度为n的整数序列,两个人轮流从左端或者右端拿数,A先取,问最后A的得分-B的得分的结果. 析:dp[i][j] 表示序列 i~j 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
随机推荐
- 【整理】Angularjs 监听ng-repeat onfinishrender事件
http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished http://ww ...
- 微信用户量破6.5亿 首超移动QQ
腾讯控股有限公司昨日公布了微信和WeChat合并月活跃用户量达到6.5亿,同比再涨39%.QQ在移动智能终端月活为6.39亿,同比增长18%,尽管势头也不错,但这是第一次,微信干掉了移动QQ! 201 ...
- cf.VK CUP 2015.C.Name Quest(贪心)
Name Quest time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 关于visio 2007导入独立图库
很多作网络拓扑或服务器系统拓扑时,我们都会找到相关的Visio图库来画,但很多时候我们不知如何才能够直接导入,下面是我自己的导入方式,供大家参考下! 打开07Visio,自动加载设置: 工具--> ...
- move 和 CopyMemory的区别
Move(ABuffer,P, Sizeof(ABuffer)); //指针传递 Move(ABuffer^,P^, Sizeof(TArrayByte)); //复制内 ...
- 最短路&&最小生成树水题
训练赛20151122 5:00:00 Overview Problem Status Rank Discuss Current Time: 2015-11-23 17:33:18 Conte ...
- 细微之处:比较两种CSS清除浮动的兼容
http://www.cnblogs.com/bienfantaisie/archive/2011/05/27/2059597.html 清除浮动是连续浮动元素之后的必备工作,在工作中我做到需要清除浮 ...
- mysql如何设置密码
1.用root 进入mysql后mysql>set password =password('你的密码');mysql>flush privileges;2.使用GRANT语句 mysql& ...
- Reverse Linked List | & ||
Reverse Linked List I Reverse a linked list. Example For linked list 1->2->3, the reversed lin ...
- 47. 数组中出现次数超过一半的数字[Number appears more than half times]
[题目]:数组中有一个数字出现的次数超过了数组长度的一半,找出这个数字. 例如长度为9的数组{1,2,3,2,2,2,5,4,2}中次数超过了数组长度的一半的数字为2,而长度为8的数组{1,2,3,2 ...