gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Now there are n

gems, each of which has its own value. Alice and Bob play a game with these
n

gems.
They place the gems in a row and decide to take turns to take gems
from left to right.
Alice goes first and takes 1 or 2 gems from the left.
After that, on each turn a player can take k

or k+1

gems if the other player takes k

gems in the previous turn. The game ends when there are no gems left or the
current player can't take k

or k+1

gems.
Your task is to determine the difference between the total value of
gems Alice took and Bob took. Assume both players play optimally. Alice wants to
maximize the difference while Bob wants to minimize it.

 
Input
The first line contains an integer T

(1≤T≤10

), the number of the test cases.
For each test case:
the first line
contains a numbers n

(1≤n≤20000

);
the second line contains n numbers: V1,V2…Vn

. (−100000≤Vi≤100000

)

 
Output
For each test case, print a single number in a line:
the difference between the total value of gems Alice took and the total value of
gems Bob took.
 
Sample Input
1
3
1 3 2
 
Sample Output
4
分析:每个人取的数目和上一个人相等或多一个,第一个人想让差值最大,第二个人最小;
   等价于两人都想最大化自己与另一个人差值;
   dp[i][j]表示当前这个人从i取数取j个的最大差值;
   转移方程为dp[i][j]=sum[i+j-1]-sum[i-1]-max(dp[i+j][j],dp[i+j][j+1]);
   考虑后者的情况,显然是取max,即最坏情况;
   最后取dp[1][1]和dp[1][2]的最大值即可;
 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define inf 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls (rt<<1)
#define rs (rt<<1|1)
#define all(x) x.begin(),x.end()
const int maxn=2e4+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=f*p%mo;p=p*p%mo;q>>=;}return f;}
int n,m,k,t,a[maxn],dp[maxn][],sum[maxn];
void upd(int &x,int y){if(x<y)x=y;}
int main(){
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]),sum[i]=sum[i-]+a[i];
int ret=-inf;
for(i=n;i>=;i--)
{
for(j=min(,n-i+);j>=;j--)
{
dp[i][j]=sum[i+j-]-sum[i-];
if(n-i-j+>=j)
{
int re=dp[i+j][j];
if(n-i-j>=j)upd(re,dp[i+j][j+]);
dp[i][j]-=re;
}
if(i==&&j<=)upd(ret,dp[i][j]);
}
}
printf("%d\n",ret);
}
return ;
}
/*
1
7
68 -1 25 59 -65 -46 -28
ans:112
*/

2017沈阳网络赛hdu6199 gems gems gems的更多相关文章

  1. HDU 6200 2017沈阳网络赛 树上区间更新,求和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6200 题意:给个图,有2种操作,一种是加一条无向边,二是查询u,v之间必须有的边的条数,所谓必须有的边 ...

  2. HDU 6199 2017沈阳网络赛 DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6199 题意:n堆石子,Alice和Bob来做游戏,一个人选择取K堆那么另外一个人就必须取k堆或者k+1 ...

  3. HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...

  4. HDU 6205 2017沈阳网络赛 思维题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6205 题意:给你n堆牌,原本每一堆的所有牌(a[i]张)默认向下,每次从第一堆开始,将固定个数的牌(b ...

  5. HDU 6198 2017沈阳网络赛 线形递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198 题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几. 解法:先暴力打表看看有没有规律 ...

  6. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  7. HDU 6197 array array array 2017沈阳网络赛 LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197 题意:给你n个数,问让你从中删掉k个数后(k<=n),是否能使剩下的序列为非递减或者非递增 ...

  8. HDU 6195 2017沈阳网络赛 公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6195 题意:有M个格子,有K个物品.我们希望在格子与物品之间连数量尽可能少的边,使得——不论是选出M个 ...

  9. HDU 6194 string string string 2017沈阳网络赛 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6194 题意:告诉你一个字符串和k , 求这个字符串中有多少不同的子串恰好出现了k 次. 解法:后缀数组 ...

随机推荐

  1. go语言笔记——defer作用DB资源等free或实现调试

    defer 和追踪 关键字 defer 允许我们推迟到函数返回之前(或任意位置执行 return 语句之后)一刻才执行某个语句或函数(为什么要在返回之后才执行这些语句?因为 return 语句同样可以 ...

  2. 65. ExtJs获取文本框中值的几种方式

    转自:https://blog.csdn.net/qiu512300471/article/details/17415675/ 1.Html文本框    如:<input type=" ...

  3. MySQL 1366错误解决办法

    MySQL 1366错误大致描述如下 SQL Error: 1366: Incorrect string value: "xE8xAFxA6xE7xBBx86-" for colu ...

  4. Android.mk添加第三方jar包(转载)

    转自:www.cnblogs.com/hopetribe/archive/2012/04/23/2467060.html LOCAL_PATH:= $(call my-dir)include $(CL ...

  5. 你真的知道GET和POST两种基本请求方法的区别吗?

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  6. $P5240 Derivation$

    神仙题. 第一场月赛的题目我到第二场月赛完了才写[由此可见我是真的菜 题目就是个大模拟加乘显然,幂的话需要将原函数.导函数的函数值用扩展欧拉定理展开 \(log\) 层.时间复杂度 \(O(T |S| ...

  7. printf的实型

    参  数 说  明 %f 按实数格式输出,整数部分按实际位数输出,6位小数 %m.nf 总位数m(含小数点),其中有n位小数 %-m.nf 同上,左对齐 %0.xf 输出小数点后x位   %f 后面如 ...

  8. Map,Filter 和 Reduce

    Map会将一个函数映射到一个输入列表的所有元素上 map(function_to_apply, list_of_inputs) items = [1, 2, 3, 4, 5] squared = li ...

  9. Axure基础教程

    文字教程:      https://www.axure.com.cn/3608/ 视频教程:      http://www.iqiyi.com/playlist409963402.html

  10. 联想 A5(L18011) 免解锁BL 免rec Magisk Xposed ROOT 救砖 ZUI 3.9.068

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...