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 时先手得分的最大值,然后两种决策,要么从左端拿,要么从 ...
随机推荐
- JDBC 操作插入表出现javax.sql.rowset.serial.SerialBlob cannot be cast to oracle.sql.BLOB
/** * 接口方法 */ public void excuteInputDB(SynchServiceConfig synchServiceConfig) throws Exception { tr ...
- ORACLE A表字段更改为B表的字段
UPDATE IM_PARA_CHECK_DATA_NEW A SET (OPERASTATE, COVER_TYPE, COVER_PRO, WORK_BAND, DEVICE_TYPE) =(SE ...
- 手搓SSM
相关资料,网上的资料很多,但是文章看不懂,看别人写好的代码比较好理解 ssm-example mysssm 整个流程和原理 一个入口类,入口类需要在tomcat启动的时候执行 通过扫描文件加把文件取出 ...
- 115、Java中String类之使用concat进行字符串连接
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 如何在django-filter中用choice field 的 value 值过滤对象
如果我们有这样一个model: class IPInfoModel(models.Model): TYPE_INTRANET = 1 TYPE_INTERNET = 2 IP_TYPES = ( (T ...
- js读取本地json/txt/xml存在跨越问题,可以用jsonp 读取本地json文件
想自己用 js写一个原生的ajax请求,访问本地文件,json/txt.但是demo,写了一个后,发现 原来是跨域了. js 写的原生ajax 请求代码如下 html代码 <div id=&qu ...
- Mybatis(使用)与Spring整合
1.总结 https://pan.baidu.com/s/1kWpz7ZD 密码:tsvr 2.代码 https://pan.baidu.com/s/1mjgAeak 密码:h9j8 3.资料 ...
- Python之第一次自夸
有一个好玩的代码 import win32com.client g = win32com.client.Dispatch("SAPI.SPVOICE") g.Speak(" ...
- No module named 'widgets'
https://blog.csdn.net/heatdeath/article/details/70313645 适配python3的. https://github.com/twz915/Djang ...
- linux动态库(.so)和静态库(.a)的区别
静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.编译之后程序文件大,但加载快,隔离性也好. 动态库在程序编译时并不会被连接到目标代码中,而是在程序运行时才被载入,因此在程序运行时 ...