因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低。  

  用$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的更多相关文章

  1. 09_Sum游戏(UVa 10891 Game of Sum)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...

  2. uva 10891 Game of Sum(区间dp)

    题目连接:10891 - Game of Sum 题目大意:有n个数字排成一条直线,然后有两个小伙伴来玩游戏, 每个小伙伴每次可以从两端(左或右)中的任意一端取走一个或若干个数(获得价值为取走数之和) ...

  3. UVa 10891 Game of Sum - 动态规划

    因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低. 用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分. 转移通过枚举取的数的个数$k$来转移.因为你希望先手得分 ...

  4. UVA - 10891 Game of Sum 区间DP

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461 Game of sum Description This ...

  5. 28.uva 10891 Game of Sum 记忆化dp

    这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解      请戳这里 ... #include<cstdio> #include<algorithm> #i ...

  6. UVA 10891 Game of Sum

    题目大意就是有一个整数串,有两个人轮流取,每次可以取走一个前缀或后缀.两人都足够聪明,且都会使自己收益最大.求取完后先手比后手多多少. 每次我看见上面那句就会深感自己的愚笨无知. 所以来推推性质? 1 ...

  7. UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  8. UVA 10891 Game of Sum(区间DP(记忆化搜索))

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. 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 ...

随机推荐

  1. cron 定时器简单入门

    cron:计划任务,是任务在约定的时间执行已经计划好的工作,根据配置文件约定的时间来执行特定的任务. 编写测试类继承 IJob ,实现Execute 此方法就是用于定时的任务 配置定时时间: 先创建w ...

  2. AX 插入一条记录提示表记录已经存在,但是该记录实际上是不存在的。

    做测试的时候遇到一个情况"AX 插入一条记录提示表记录已经存在,但是该记录实际上是不存在的." 检查到该表(TABLE_ABC)所有的key都是AllowDuplicate的, 继 ...

  3. nginx相关的一些记录

    http redirect to https: if ($http_cf_visitor ~ '"scheme":"http"'){ rewrite ^/(.* ...

  4. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  5. leetcode 198

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  6. .NET (五)委托第五讲:内置委托Predicate

    // 摘要: // 表示定义一组条件并确定指定对象是否符合这些条件的方法. // // 参数: // obj: // 要按照由此委托表示的方法中定义的条件进行比较的对象. // // 类型参数: // ...

  7. 加载AssetBundle方法

    先介绍一种常用的加载AssetBundle方法 using UnityEngine; using System.Collections; using System.IO; public class L ...

  8. Lua 5.2 编译 For Windows

    body { font-family: 微软雅黑; font-size: 11pt; line-height: 1.5; } html, body { color: #000000; backgrou ...

  9. etcd api 接口

    etcd api接口 基本操作api: https://github.com/coreos/etcd/blob/6acb3d67fbe131b3b2d5d010e00ec80182be4628/Doc ...

  10. CVPR 2007 Learning to detect a salient object

    Dataset: MSRA A&B are introduced in this paper. A conditional Random Field based method was prop ...