gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1778    Accepted Submission(s): 424

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
 
Source
 

题意:

Alice 和 Bob 玩游戏,有n堆宝石,每一堆都有价值,如果前一个人拿了k堆那么下一个人能够拿k堆或者k+1堆,两个人都希望自己拿到的价值与另一个人的差最大,求最后价值Alice-Bob,Alice可以先拿1堆或者2堆。

代码:

//看似博弈其实是dp,由于从第一个人开始并且他只能拿1或2堆,所以这是终结状态,从后向前推,设f[0/1][i][j]表示第1/2个人从第i堆开始拿并
//且上一个人拿了j堆的最优方案(Alice拿到的-Bob拿到的),每一个人又有两种拿法即j,j+1,所以处理一下前缀和转移方程就出来了。两个人都希
//望最优即第一个人希望(Alice-Bob)最大,第二个人希望(Bob-Alice)最大,我们可以把后一项看成(Alice-Bob)最小。
//因为k最大不超过200(2+4+...+k<=n),但是数组还是太大,要滚动数组。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MOD=;
int f[][][],sum[];
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
sum[]=;
memset(f,,sizeof(f));
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<=;j++){
if(i+j<=n){
f[][i%MOD][j]=max(sum[i+j-]-sum[i-]+f[][(i+j)%MOD][j],sum[i+j]-sum[i-]+f[][(i+j+)%MOD][j+]);
f[][i%MOD][j]=min(sum[i-]-sum[i+j-]+f[][(i+j)%MOD][j],sum[i-]-sum[i+j]+f[][(i+j+)%MOD][j+]);
}else if(i+j-<=n){
f[][i%MOD][j]=sum[i+j-]-sum[i-]+f[][(i+j)%MOD][j];
f[][i%MOD][j]=sum[i-]-sum[i+j-]+f[][(i+j)%MOD][j];
}
}
}
printf("%d\n",f[][][]);
}
return ;
}

HDU 6199 DP的更多相关文章

  1. HDU 6199 DP 滚动数组

    强行卡内存 这题在CF上好像有道极相似的题 可以想到状态设计为dp[f][i][k]表示f在取完i-1时,此时可以取k个或k+1个的状态下的最大值.之前以为n是1e5,自己想不到怎么设计状态真的辣鸡, ...

  2. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  4. HDU 6199 2017沈阳网络赛 DP

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

  5. hdu 6199 沈阳网络赛---gems gems gems(DP)

    题目链接 Problem Description Now there are n gems, each of which has its own value. Alice and Bob play a ...

  6. hdu 6199 gems gems gems dp

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

  7. HDU - 6199 gems gems gems (DP)

    有n(2e4)个宝石两个人轮流从左侧取宝石,Alice先手,首轮取1个或2个宝石,如果上一轮取了k个宝石,则这一轮只能取k或k+1个宝石.一旦不能再取宝石就结束.双方都希望自己拿到的宝石数比对方尽可能 ...

  8. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. Harbor配置https,并安装内容信任插件(notary)

    1.配置https https://github.com/goharbor/harbor/blob/master/docs/configure_https.md 2.harbor安装notary插件 ...

  2. java之接口开发-初级篇-socket通信

    socket通信实现util包类实现 public class SocketThread extends Thread { public void run() { while (true) { // ...

  3. Tree - Gradient Boosting Machine with sklearn source code

    This is the second post in Boosting algorithm. In the previous post, we go through the earliest Boos ...

  4. hadoop HA sshfen切换隔离时无法跳转ssh: bash: fuser: 未找到命令

    在zkfc的日志里面,有一个warn:PATH=$PATH:/sbin:/usr/sbin fuser -v -k -n tcp 8090 via ssh: bash: fuser: 未找到命令原因是 ...

  5. scrum立会报告+燃尽图(第三周第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...

  6. mysql 多查询临时表的运用

    SELECT * from (select count(*) imgCount1 from imagetable where SeriesID = '1201061992020630292018092 ...

  7. 寒假学习计划(C++)

    课程 1,计算机程序设计(C++)-西安交通大学(中国大学mooc)课程链接 2,面向对象程序设计-C++-浙大-翁恺(网易云课堂)课程链接 理由 1西安交大的C++慕课从零基础教起,更注重基础,重点 ...

  8. ltnmp 3.0 发布,PHP 开发环境一键安装包

    PHP 开发环境一键安装包, 有个叫lnmp.这个ltnmp看起来更新比较多,开发比较频繁,包括的组件更多. 安装和使用教程:http://www.moqifei.com/ltnmp 标记一下.

  9. Objective - C 之类目

    一.类目(category):为已有的类(可以是系统类,也可以是自定义类)添加公有的新的方法: 例如:为系统已有的NSString类添加一个比较字符串大小的方法 1.创建过程: 2.NSString ...

  10. 理解promise 01

    原文地址: http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html 用Javascript的小伙伴们,是时候承认了,关于 ...