题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少。

分析:

1、枚举先手拿的分界线,要么从左端拿,要么从右端拿,比较得最优解。

2、dp(i, j)---在区间(i, j)中A最多比B多拿多少。

3、tmp -= dfs(i + 1, r);//A拿了区间(l, i),B在剩下区间里尽可能拿最优

tmp是A拿的,dfs(i + 1, r)是B比A多拿的,假设dfs(i + 1, r)=y-x,y是B拿的,x是A拿的

则tmp-dfs(i + 1, r) = tmp - y + x,也就是最终A比B多拿的。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int sum[MAXN];
int dp[MAXN][MAXN];
int dfs(int l, int r){
if(dp[l][r] != INT_INF) return dp[l][r];
int diff = sum[r] - sum[l - 1];
for(int i = l; i < r; ++i){//先手从左端拿
int tmp = sum[i] - sum[l - 1];
tmp -= dfs(i + 1, r);//后手从右端拿
if(tmp > diff) diff = tmp;
}
for(int i = l; i < r; ++i){
int tmp = sum[r] - sum[i];
tmp -= dfs(l, i);
if(tmp > diff) diff = tmp;
}
return dp[l][r] = diff;
}
int main(){
int n;
while(scanf("%d", &n) == 1){
if(!n) return 0;
memset(dp, INT_INF, sizeof dp);
sum[0] = 0;
for(int i = 1; i <= n; ++i){
scanf("%d", &sum[i]);
sum[i] += sum[i - 1];
}
printf("%d\n", dfs(1, n));
}
return 0;
}

  

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

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

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

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

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

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

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

  6. uva 10003 Cutting Sticks 【区间dp】

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

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

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

  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. C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据

    练习8.1:编写函数.接受一个istream&参数,返回值类型也是istream&.此函数必须从给定流中读取数据,直至遇到文件结束标识时停止. #include <iostrea ...

  2. 「luogu2617」Dynamic Rankings

    「luogu2617」Dynamic Rankings 传送门 树套树直接上树状数组套主席树,常数很大就是了. 树套树参考代码: /*-------------------------------- ...

  3. springboot 时间戳和 数据库时间相差14个小时

    在 springboot 开发过程中遇到一个奇怪的问题,就是已经设置系统时间GMT+8, 但是时间到数据库后会减少14个小时.后来发现是 jvm 时区和数据库时区设置不一致的问题. jvm 设置的是 ...

  4. nrm 源管理器

    什么是nrm nrm 是一个 npm 源管理器,允许你快速地在 npm 源间切换. 安装nrm 在命令行执行命令,npm install -g nrm,全局安装nrm. 使用 执行命令nrm ls查看 ...

  5. day07 集合

    ''' list,查询过程中修改,会报错,类似java的并发修改异常 Traceback (most recent call last): File "C:/1xubenqing/pytho ...

  6. MariaDB——备份与恢复

    备份和恢复 为什么要备份?   灾难恢复:硬件故障.软件故障.自然灾害.黑客攻击.误操作   测试   要注意的点:   备份需要多少时间   能够容忍多少的数据丢失   恢复数据需要在多长时间完成  ...

  7. httpclient访问接口步骤

    1. 创建HttpClient对象. 2. 构造Http 请求对象. 3. 执行HttpClient对象的execute方法,将Http请求对象作为该方法的参数. 4. 读取execute方法返回的H ...

  8. UOJ192 最强跳蚤

    题目链接 problem 给出一个n个点带边权的树,问有多少对\((u,v)\)满足\(u\)到\(v\)路径上边权的乘积为完全平方数. \(n\le 10^5,w\le 10^8\) solutio ...

  9. centos7下安装JDK1.8

    步骤1:Oracle下载jdk-8u141-linux-x64安装包 步骤2:解压jdk-8u141-linux-x64.tar.gz到home目录 [root@model ~]# tar -zxvf ...

  10. POI 2001 Goldmine 线段树 扫描线

    题目链接 http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=1350 http://main.edu.pl/en/archive/oi/8/kop ...