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 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
随机推荐
- mac下删除不需要的应用程序
一般的应用程序删除: 1)可以在 前往--应用程序 中直接删除 2)直接在启动台中按住出现X直接删除. 问题: mac下不出现在应用程序中,启动台中按住也不出现X,也不可以直接拖到废纸篓中删除的应用如 ...
- Android之Builder对话框的一些常用方式
原文: http://blog.csdn.net/kkfdsa132/article/details/6322835 Android为我们提供几种对话框,主要有:AlertDialog.Progres ...
- 0-1背包问题(0-1 knapsack problem)
0-1背包问题描述:一个正在抢劫商店的小偷发现了n个商品,第i个商品价值 vi 美元,重 wi 磅,vi 和 wi 都是整数.这个小偷希望拿走价值尽量高的商品,但他的背包最多能容纳 S 磅重的商品,S ...
- vue-router重定向redirect
- Ansible ssh-key密钥认证配置
对于被管理服务器做免密码登录设置 1.在管理服务器生成ssh-key密钥 #ssh-keygen //生成秘钥 root@hsz:/etc/ansible# ssh-keygen Generatin ...
- Spring MVC中的ResponseEntity和ResponseBody的区别
1.ResponseEntity的优先级高于@ResponseBody. 在不是ResponseEntity的情况下才去检查有没有@ResponseBody注解. 如果响应类型是ResponseEnt ...
- django 模版内置的过滤器
一.add 将传进来的参数添加到原来的值上面.这个过滤器会尝试将“值”和“参数”转换成整形然后进行相加.如果转换成整形过程中失败了,那么将会将“值”和“参数”进行拼接.如果是字符串,那么会拼接成字符串 ...
- mysql时出现:is not allowed to connect to this MySQL serverConnection closed by foreign host问题的解决
这个原因是因为索要链接的mysql数据库只允许其所在的服务器连接,需要在mysql服务器上设置一下允许的ip权限,如下: 1.连接mysql mysql -u root -p 1 如图: 2.授权 g ...
- [Linux] day06——文档管理
文档管理===================mkdir 创建目录 -p /路径/目录名 (父路径不存在 -p) --------------------------------------- t ...
- hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数
Cow Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...