UVA - 10891 Game of Sum (区间dp)
题意: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)的更多相关文章
- uva 10891 Game of Sum(区间dp)
题目连接:10891 - Game of Sum 题目大意:有n个数字排成一条直线,然后有两个小伙伴来玩游戏, 每个小伙伴每次可以从两端(左或右)中的任意一端取走一个或若干个数(获得价值为取走数之和) ...
- UVA - 10891 Game of Sum 区间DP
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461 Game of sum Description This ...
- 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 ...
- 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 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
随机推荐
- SpringBoot之基础入门-专题一
SpringBoot之基础入门-专题一 一.Spring介绍 1.1.SpringBoot简介 在初次学习Spring整合各个第三方框架构建项目的时候,往往会有一大堆的XML文件的配置,众多的dtd或 ...
- uniGUI 应用程序体系结构(11)
下图表示 uniGUI 服务器的内部结构. 每个 uniGUI 服务器都有一个ServerModule的副本, 每台服务器创建一次, 同时根据用户活动动态创建和销毁多个Session. uniGUI ...
- Xcode忽略编译警告
关于本文: 1.说明让Xcode忽略编译警告的目的 2.关于编译警告的开启与关闭的实现 3.顺便借此机会宣传下理解编译原理的大大好处 临时有事,后面更新.
- SVN commit,update用法
https://blog.csdn.net/studyvcmfc/article/details/4528896
- 【剑指Offer面试编程题】题目1386:旋转数组的最小数字--九度OJ
题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...
- Day1学习总结
# 1.print()# 2.input()# 3.if:# elif# else#4.while循环#5.for i in range()#6.break.continue#7.import ran ...
- C# 篇基础知识1——编译、进制转换、内存单位、变量
编译:C#语言要经过两次编译,程序员编写好源代码后进行第一次编译,将源代码编译为微软中间语言(MSIL),生成可以发布的应用软件:当用户使用软件时,MSIL代码会在首次载入内存后进行第二次编译,中间语 ...
- MySQL 加密 压缩
许多MySQL加密和压缩函数返回结果可能包含任意字节值的字符串,如果要存储这些结果,请使用VARBINARY或BLOB二进制字符串数据类型.这将避免使用非二进制字符串数据类型(CHAR, VARCHA ...
- Lesson 46 Hobbies
Who, according to the authour, are 'Fortune's favoured children'? A gifted American psychologist has ...
- Python @函数装饰器及用法
1.函数装饰器的工作原理 函数装饰器的工作原理是怎样的呢?假设用 funA() 函数装饰器去装饰 funB() 函数,如下所示: #funA 作为装饰器函数 def funA(fn): #... fn ...