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

    http://www.lydsy.com/JudgeOnline/problem.php?id=4889 人傻常数大 bzoj上跑不过 洛谷上能过两到三个点 我写的是树套树啊 怎么跑的比分块还慢 每次 ...

  2. 八大排序算法(Python)

    一.插入排序 介绍 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的.个数加一的有序数据.      算法适用于少量数据的排序,时间复杂度为O(n^2).      插入 ...

  3. codevs1163访问艺术馆(树形dp)

    1163 访问艺术馆  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description 皮尔是一个出了名的盗画者,他经过数月的精心准备, ...

  4. IE版本的判断

    var Sys = {};var ua = navigator.userAgent.toLowerCase(); var s;(s = ua.match(/msie ([\d.]+)/)) ? Sys ...

  5. 3最短路的几种解法 ------例题< 最短路 >

    点击进入例题   最短路 我知道的有三种方法 1 : 深搜 每次  每次有更小的路径时  就更新   ,   2 :   Dijkstra    3 : floyd 前两种   是  单源 最短路径 ...

  6. c++小游戏

    #include <iostream> using namespace std; double shengmingli=2000;//定义主角初始生命力 int gongjili=150; ...

  7. sqlyog注册码激活

    姓     名(Name):ttrar 序 列 号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c 或者(OR) 姓     名(Name):ttrar 序 列 ...

  8. [转]Linux之ACL权限

    转自:http://www.2cto.com/os/201110/108736.html 引言 前面的内容中,我们讲到传统的权限仅有三种身份(owner,group,others)搭配三种权限(r,w ...

  9. 【转】Linux 之 数据流重定向

    转自:http://www.linuxidc.com/Linux/2012-09/69764.htm linux在你登入时,便将默认的标准输入.标准输出.标准错误输出安排成你的终端.I/O重定向就是你 ...

  10. Html学习-File控件学习

    情况一:不设置enctype HTML内容 <form action="02Upload.ashx" method="post"> <inpu ...