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 ...
随机推荐
- angularJS module里的'服务'
首先,为了举栗子,先写好如下的模型,控制器,html: html: <!DOCTYPE html> <html ng-app="serviceApp"> & ...
- iOS多线程编程之多线程简单介绍(转载)
一.进程和线程 1.什么是进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过“ ...
- 看病要排队--hdu1873
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1873 运用优先队列写就行了 #include<stdio.h> #include< ...
- hive-site.xml配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-s ...
- 网站/IIS/Web/WCF服务 访问共享目录 映射 的解决方案
目录 问题案例 原因分析 解决问题 总结 问题案例 环境: 电脑A:winform程序: 电脑B:部署了一个文件上传的WCF服务在IIS上.且该服务的配置文件中已经增加 <identity im ...
- Bug笔记:Google Map第一次缩放位置偏移
这是个让人蛋疼的bug,认真查看Google maps API文档的童鞋们一定不会碰到. 我的同事为项目写了个针对map这块的jQuery plugin,然后在项目测试中发现,刚加载完页面时,直接点击 ...
- iOS10网络权限数据
参考地址:1.http://www.cocoachina.com/ios/20180723/24274.html https://blog.csdn.net/wang_bo_justone/art ...
- 004-spring cloud gateway-网关请求处理过程
一.网关请求处理过程 客户端向Spring Cloud Gateway发出请求.如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序.此处理程序运行通过特定于请求的过滤器链发送请求. ...
- PHP高并发和大流量的解决方案
第一个要说的就是数据库,首先要有一个很好的架构,查询尽量不用* 避免相关子查询 给经常查询的添加索引 用排序来取代非顺序存取,如果条件允许 ,一般MySQL服务器最好安装在Linux操作系统中 .关于 ...
- SQL Expression Language Tutorial 学习笔记二
11. Using Textual SQL 直接使用 SQL 如果实在玩不转, 还是可以通过 test() 直接写 SQL. In [51]: s = text( ...: "SELECT ...