题目连接: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的更多相关文章

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

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

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

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

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

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

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

  5. uva 10003 Cutting Sticks 【区间dp】

    题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,事实上和石子归并是一样的 ...

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

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

  7. UVA - 10891 Game of Sum (区间dp)

    题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...

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

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

  9. UVa 10891 Game of Sum (DP)

    题意:给定一个长度为n的整数序列,两个人轮流从左端或者右端拿数,A先取,问最后A的得分-B的得分的结果. 析:dp[i][j] 表示序列 i~j 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...

随机推荐

  1. 使用guava带来的方便

    ​    ​guava是在原先google-collection 的基础上发展过来的,是一个比较优秀的外部开源包,最近项目中使用的比较多,列举一些点.刚刚接触就被guava吸引了... ​    ​这 ...

  2. ubuntu下手把手教你搭建SVN服务器

    序,我的ubuntu服务器版本是14.04 ,x64(64位)操作系统,服务器在国内. 目录 一.安装SVN服务器 1 安装svn2 创建目录3 创建版本仓库4 配置5 启动SVN服务器6 验证svn ...

  3. Nginx安装与配置文件解析

    导读 Nginx是一款开放源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3代理服务,是一款自由的软件,同时也是运维工程师必会的一种服务器,下面我就简单的说一下Nginx服务器的 ...

  4. characterCustomezition的资源打包代码分析

    using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; class Creat ...

  5. SVN安装与配置 SVN整合MyEclipse

    SVN安装: 1.安装服务器 ######### 安装文件:SVN服务器############### # http://www.collab.net/downloads/subversion # C ...

  6. HTML前端

    1.<html>内容</html> 解释:HTML文档的文档标记,也成为HTML开始标记 功能:这对标记分别位于网页的最前端和最后端 <html>在最前段表示网页的 ...

  7. 默认hosts后面为files dns

    售后工程师 : 您好,问题已经解决,问题原因是您修改了/etc/nsswitch.conf配置文件中的hosts:这项导致的,默认hosts后面为files dns,但是后面去掉了DNS导致直接使用本 ...

  8. 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续

    目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...

  9. HTML模仿桌面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. ffplay mini 媒体播放器

    下载 http://pan.baidu.com/s/1dDcp3lZ 一定要解压到 D:\ffplay\ 目录下 双击 OpenWith_FFPlay_mini.reg 注册ffplay 在视频文件名 ...