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 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
随机推荐
- CSS创建一个遮罩层
.layer{ width: 100%; position: absolute; left:; right:; top:; bottom:; -moz-opacity:; filter: alpha( ...
- RTMP流媒体播放过程(转)
本文描述了从打开一个RTMP流媒体到视音频数据开始播放的全过程. 注意:RTMP中的逻辑结构 RTMP协议规定,播放一个流媒体有两个前提步骤:第一步,建立一个网络连接(NetConnection):第 ...
- Ubuntu 12.04 安装 Chrome浏览器
1,先到chrome官网下载一个安装包 http://www.google.com/intl/zh-CN/chrome/ 2,ctrl+alt+t 打开终端. 3,在终端里输入sudo apt-get ...
- systemctl 命令的用法
对比表,以 apache / httpd 为例 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 httpd on systemctl enable httpd.serv ...
- 一个PHP写的简单webservice服务端+客户端
首先是服务端,服务端有一个主要的class组成:apiServer.php <?php /** * apiServer.php * * webservice主类 * * @filename ap ...
- 异常:The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml or the jar files deployed with this application
The absolute uri: http://www.springframework.org/security/tags cannot be resolved in either web.xml ...
- CF#310 d2
A:|c[1]-c[0]| B:A+-(oc)A[0]==0..n-1 C: #include <cstdio> int n,m,i,j,k,p; int ll,ca,cb,cc; int ...
- TCP/IP详解学习笔记(5)-- ICMP:internet 控制报文协议
1.概述 ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制 ...
- .Net Attribute特性
1.什么是Atrribute 首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标 ...
- Ribbon_窗体_实现Ribbon风格的窗体
Ribbon_窗体_实现Ribbon风格的窗体 随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢? ...