题目链接

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. 蓝桥杯比赛javaB组练习《方格填数》

    方格填数 如下的10个格子   +--+--+--+   |  |  |  |+--+--+--+--+|  |  |  |  |+--+--+--+--+|  |  |  |+--+--+--+ ( ...

  2. C#设计模式总结(转)

    一.引言 经过这段时间对设计模式的学习,自己的感触还是很多的,因为我现在在写代码的时候,经常会想想这里能不能用什么设计模式来进行重构.所以,学完设计模式之后,感觉它会慢慢地影响到你写代码的思维方式.这 ...

  3. 使用CHCA搭建静态博客

    [toc] chca是一个使用golang开发的静态博客生成器,简单.方便.快捷,抛弃每次都需要使用命令编译文件,采用文件监听方式编译,作者只需把markdown文件放到配置中的markdown文件夹 ...

  4. 简陋的 ASP.NET CORE 单页Web应用程序“框架”

    我对ASP.NET CORE了解不多,不知道是不是重复造轮子,也或者什么也不是,这个Demo是这样的: 1.非常简单或者说原始:2.将单页Web应用增加了一个页(Page)概念(相当于MVC的 Vie ...

  5. HDU 6097---Mindis(二分)

    题目链接 Problem Description The center coordinate of the circle C is O, the coordinate of O is (0,0) , ...

  6. 让你快速了解并掌握如何进行iOS开发技能

    首先你要花点时间针对objective-c语言的学习:毕竟这个是iOS开发的基础(你也可以尝试用Swift,但此项目只是针对OC),编程套路其实都是差不多,多写多想多实践:关于环境的搭建就不在本文进行 ...

  7. Windows开发笔记-错误处理

    Windows函数为开发者提供了错误代码,这样当调用Windows函数失败后,可以通过查看错误代码了解错误信息.相应的错误与主调线程关联在一起,这种机制使得不同的线程能够独立运行而不会相互干扰.调用G ...

  8. RMAN基础恢复测试

    --RMAN恢复测试实战   RMAN> list backup;   using target database control file instead of recovery catalo ...

  9. NSThread创建线程的三种方法

    - (IBAction)NSThreadBtnClick:(id)sender { [self threadObjectMethod]; [self threadClassMethod]; [self ...

  10. C++中的继承(3)作用域与重定义,赋值兼容规则

    作用域与重定义(同名隐藏) 一样的,先上代码 1 class A 2 { 3 public: 4 int a_data; 5 void a() 6 { 7 cout << "A& ...