UVa 10891 Game of Sum - 动态规划

因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低。
用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分。
转移通过枚举取的数的个数$k$来转移。因为你希望先手得分尽量高,所以另一个人的最高得分应尽量少。
$dp[i][j] = sum[i][j] - \min \{dp[i + k][j],dp[i][j - k]\}$
但是发现计算$dp[i + k][j],dp[i][j - k]$的最小值的地方很重复,所以用一个$f[i][j]$储存前者的最优值,$g[i][j]$储存后者的最优值。
这样就将代码的时间复杂度优化到O(n2)
Code
/**
* uva
* Problem#10891
* Accepted
* Time:0ms
*/
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
typedef bool boolean;
#define INF 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
template<typename T>
inline void readInteger(T& u){
char x;
long long aFlag = ;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} int n;
int *list;
int f[][];
int g[][];
int dp[][]; inline boolean init(){
readInteger(n);
if(n == ) return false;
list = new int[(const int)(n + )];
for(int i = ; i <= n; i++){
readInteger(list[i]);
}
return true;
} int *sum;
inline void getSum(){
sum = new int[(const int)(n + )];
sum[] = ;
for(int i = ; i <= n; i++)
sum[i] = sum[i - ] + list[i];
} inline void solve(){
memset(f, 0x7f, sizeof(f));
memset(g, 0x7f, sizeof(g));
for(int i = ; i <= n; i++) f[i][i] = g[i][i] = dp[i][i] = list[i];
for(int k = ; k < n; k++){
for(int i = ; i + k <= n; i++){
int j = i + k;
int m = ;
smin(m, f[i + ][j]);
smin(m, g[i][j - ]);
dp[i][j] = sum[j] - sum[i - ] - m;
f[i][j] = min(f[i + ][j], dp[i][j]);
g[i][j] = min(g[i][j - ], dp[i][j]);
}
}
printf("%d\n", dp[][n] * - sum[n]);
delete[] list;
delete[] sum;
} int main(){
while(init()){
getSum();
solve();
}
return ;
}
UVa 10891 Game of Sum - 动态规划的更多相关文章
- UVa 10891 - Game of Sum 动态规划,博弈 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 09_Sum游戏(UVa 10891 Game of Sum)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...
- uva 10891 Game of Sum(区间dp)
题目连接:10891 - Game of Sum 题目大意:有n个数字排成一条直线,然后有两个小伙伴来玩游戏, 每个小伙伴每次可以从两端(左或右)中的任意一端取走一个或若干个数(获得价值为取走数之和) ...
- [题解]UVa 10891 Game of Sum
在游戏的任何时刻剩余的都是1 - n中的一个连续子序列.所以可以用dp[i][j]表示在第i个数到第j个数中取数,先手的玩家得到的最大的分值.因为两个人都很聪明,所以等于自己和自己下.基本上每次就都是 ...
- UVA 10891 Game of Sum
题目大意就是有一个整数串,有两个人轮流取,每次可以取走一个前缀或后缀.两人都足够聪明,且都会使自己收益最大.求取完后先手比后手多多少. 每次我看见上面那句就会深感自己的愚笨无知. 所以来推推性质? 1 ...
- 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
题目连接: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 ...
- 28.uva 10891 Game of Sum 记忆化dp
这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解 请戳这里 ... #include<cstdio> #include<algorithm> #i ...
随机推荐
- Python的浅拷贝与深拷贝
定义: =号浅拷贝:在Python中对象的赋值其实就是对象的引用.copy了之后两个仍然是同一个东西.那么他们内部的元素自然也是一样的,对其中一个进行修改,另一个也会跟着变> copy()浅拷贝 ...
- python 几个重要的概念
转自:http://www.cnblogs.com/aylin/p/5601969.html
- 20144306《网络对抗》MAL_PC平台逆向破解_Advanced
PC平台逆向破解_Advanced 一.注入shellcode并执行 1.什么是shellcode? shellcode顾名思义就是一段为了获取交互式shell的机器指令,是用来发送到服务器利用特定漏 ...
- linux桌面与命令模式切换
在图形下面按Ctrl+alt+F1(F2\F3\F4)进入命令模式 在命令模式下,按Ctrl+alt+F7回到图形,或登录用户输入startx进入图形!
- fork 了别人的仓库后,如何将自己的代码和原仓库保持一致
fork 了别人的仓库后,如何将自己的代码和原仓库保持一致 git remote add upstream http:// git fetch upstream
- dedecms网站迁移时记得将安装目录放空 附迁移的正确方法
这段时间在赶一些新项目,我们建站一般都在本地服务器搭建起来,测试得差不多了才传到网上,这样对蜘蛛也相对友好一些,要不然改来改去变化太大给搜索引擎的第一印象很不好.但是由于本地环境和服务器环境还是有一些 ...
- context、config
Tomcat启动时已经创建了context,并使用它读取了web.xml中的参数,后台可以从context里获取参数 后台获取参数代码: ServletContext context = getSer ...
- java map.entry
我希望要一个ArrayList<Entry>,类似C++中的pair, 但是Map.Entry是个接口,不能实例化,可以像下面这样写 HashMap<Integer, Integer ...
- [LeetCode] 193. Valid Phone Numbers_Easy tag: Bash
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- checkBox的使用和事件监听
直接上代码: <!DOCTYPE html> <html> <head> <title></title> </head> < ...