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 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
随机推荐
- VO(DTO)模式在架构设计中是否需要
DTO(VO):也就是一般意义上的VO,封装后的对象.一般用于Web层—Service层间的数据传输入. PO:也就是一般概念上的Domain Object,如hibernate 中的Entity.一 ...
- php中array_flip数组翻转
array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失. 如果原数组中的值的数据类型不是字符串或整数,函数将报错. Return ...
- curl模拟post请求
1,curl -d "userType=seller&userId=1034285" "www.baidu.com/getInfo.php" curl ...
- HTTP 304 的理解
304 的标准解释是:Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...
- 第10章 使用Apache服务部署静态网站
章节简述: 本章节中通过对比目前热门的网站服务程序来说明Apache服务程序的优势,并新增主机空间选购技巧小节. 了解SELinux服务的3种工作模式,小心谨慎的使用semanage命令和setseb ...
- iATKOS v7硬盘安装教程(硬盘助手+变色龙安装版)
这是作者:Tong 写的一篇安装教程 首先感谢:wowpc制作的变色龙安装版.iATKOS作者以及硬盘安装助手作者 前言:现在时代在进步,系统同样也在进步,在以前要在PC上整个Mac是很痛苦的事情,就 ...
- HDU 2955 Robberies 背包概率DP
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- SpringMVC配置easyui-datagrid
SprimgMVC的UserController.java @RequestMapping(value = "listUserForJson") @ResponseBody pub ...
- vista/win7/win8区别
1. Vista的内核版本号是:Windows 6.0: Windows 7的内核是:Windows 6.1: Windows 8的内核是:Windows 6.2 ...
- docker的四种网络模式
/* 1. host模式 : docker run 使用 --net=host指定 docker使用的网络实际上和宿主机一样 2. container模式: 使用 --net=container:co ...