题目链接

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
 
 
题意:现在有n块宝石排成一行,价值为v[1~n]。 现在Alice和Bob 从左至右轮流取宝石,Alice先取,可以取1或2个,然后Bob取……如果前一个人取了k个,那么当前这个人只能取k或者k+1个宝石。(结束条件,如果前一个人取了k个宝石,而剩下的少于k个则停止不用取;如果剩余k个,那么这个人必须取k个),现在Alice和Bob都想得到的宝石价值和比对方尽量多,求Alice得到的价值和 - Bob得到的价值和的最大值?
 
思路:动态规划,定义dp[i][k][j], k取值0和1(0表示Alice取,1表示Bob取),i表示第i块宝石(1<=i<=20000),j表示从第i块宝石开始向右取j块宝石(1<=j<=200, 因为递增的取宝石,一个人一次最多取200块宝石),所以dp[i][k][j]表示取完i~n块宝石时(由后向前推),第k个人从i开始向右连续取k个宝石两人的价值差;
         另外,本题空间限制比较紧,所以需要使用滚动数组,第一维设为205即可。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=;
const int M=;
int sum[N];
int dp[M][][]; int main()
{
int T; cin>>T;
while(T--)
{
sum[]=;
memset(dp,,sizeof(dp));
int n; scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&sum[i]),sum[i]+=sum[i-];
for(int i=n;i>=;i--)
{
for(int j=;j<M;j++)
{
if(i+j->n) break; int tmp=sum[i+j-]-sum[i-], t;
if(i+j+j<=n) t=min(dp[(i+j)%M][][j],dp[(i+j)%M][][j+]);
else if(i+j+j-<=n) t=dp[(i+j)%M][][j];
else t=;
dp[i%M][][j]=tmp+t; if(i+j+j<=n) t=max(dp[(i+j)%M][][j],dp[(i+j)%M][][j+]);
else if(i+j+j-<=n) t=dp[(i+j)%M][][j];
else t=;
dp[i%M][][j]=t-tmp;
}
}
int ans;
if(n>=) ans=max(dp[][][],dp[][][]);
else ans=dp[][][];
printf("%d\n",ans);
}
return ;
}

hdu 6199 沈阳网络赛---gems gems gems(DP)的更多相关文章

  1. hdu 6194 沈阳网络赛--string string string(后缀数组)

    题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...

  2. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  3. 沈阳网络赛 F - 上下界网络流

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  4. 2017沈阳网络赛hdu6199 gems gems gems

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

  5. HDU 6199 2017沈阳网络赛 DP

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

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

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

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

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

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

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

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

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

随机推荐

  1. HDU-1495 非常可乐 (嵌套结构体-广搜 对比 一般广搜)

    题意 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但 ...

  2. matlab怎么查看已安装哪些工具箱和…

    问题描述:matlab怎么查看已安装哪些工具箱和它们相应的版本 解决方法:在命令行里敲击der,回车 效果:

  3. LoadRunner入门(一)

    以LR自带的web系统为例(前提条件:已安装好lordrunner 11 ): 一.WebTours系统 是lordrunner自带一个飞机订票系统网站,支持IE浏览器 1. WebTours服务启动 ...

  4. 一个想法照进现实-《IT连》创业项目:聊聊IT连App是如何思考解决IT人员单身问题的

    前言: 根据最早我编写的IT联盟社区众筹计划书的思路方向:社交->资讯=>评级=>培训. 现在在实现第一个阶段中,而且这个阶段可能会走很久. 今天开文,主要是讲述一下,现在的版本为什 ...

  5. Python初学时购物车程序练习实例

    不多说了,直接上代码: #Author:Lancy Wu product_list=[ ('Iphone',5800), ('Mac Pro',9800), ('Bike', 800), ('Watc ...

  6. QT 延时函数设置

    QT 的延时函数分为非阻塞延时 和 阻塞型延时 非阻塞延时: void GreenPass3::delaymsec(int msec){    QTime dieTime = QTime::curre ...

  7. re模块的结果小练习题

    1.匹配标签 import re ret = re.search('<(?P<tag_name>\w+)>\w+</(?P=tag_name)>','<h1& ...

  8. 使用TensorFlow创建第变量定义和运行方式

    import tensorflow as tf# 熟悉tensorflow的变量定义和运行方式v1 = tf.Variable(2) #定义变量并给变量赋值v2 = tf.Variable(48) c ...

  9. uploadify 在chrome上崩溃的解决办法

    使用Uploadify进行文件上传,Chrome经常会报“喔唷,崩溃啦”的错误,见下图: 很显然,这是Chrome浏览器缓存功能,所导致的问题.从LOG里也能看到:正常的情况下,会请求文件(jquer ...

  10. APP软件半成品测试技巧

    由于现在产品类型的多样性,产品功能的先进性,更多体现在产品质量的稳定性和可靠性.软件应用的领域不断深入,设计的复杂程度逐步扩大,开发的周期不断缩短,质量的要求就逐渐提高.然而根据我们公司的版本迭代速度 ...