hdu 6199 gems gems gems dp
gems gems gems
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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)
3
1 3 2
思路:dp,蜜汁题意;滚动数组优化空间;
#include<bits/stdc++.h>
using namespace std; const int N=2e4+,M=2e6+,inf=1e9+; int dp[][][],n,sum[N]; int main()
{
int T,x;
scanf("%d",&T);
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&x),sum[i]=sum[i-]+x;
for(int i=n;i>=;i--)
{
for(int j=;j>=;j--)
{
if(i+j<=n)
{
dp[][i%][j]=max(sum[i+j-]-sum[i-]+dp[][(i+j)%][j],sum[i+j]-sum[i-]+dp[][(i+j+)%][j+]);
dp[][i%][j]=min(-sum[i+j-]+sum[i-]+dp[][(i+j)%][j],-sum[i+j]+sum[i-]+dp[][(i+j+)%][j+]);
}
else if(i+j-<=n)
{
dp[][i%][j]=dp[][(i+j)%][j]+sum[i+j-]-sum[i-];
dp[][i%][j]=dp[][(i+j)%][j]-sum[i+j-]+sum[i-];
}
}
}
printf("%d\n",dp[][][]);
}
return ;
}
gems gems gems
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 625 Accepted Submission(s): 77
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.
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)
3
1 3 2
hdu 6199 gems gems gems dp的更多相关文章
- HDU 6199 DP 滚动数组
强行卡内存 这题在CF上好像有道极相似的题 可以想到状态设计为dp[f][i][k]表示f在取完i-1时,此时可以取k个或k+1个的状态下的最大值.之前以为n是1e5,自己想不到怎么设计状态真的辣鸡, ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1231.最大连续子序列-dp+位置标记
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
- HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)
HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...
- HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛
普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...
- 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 ...
随机推荐
- [转载]ASP.NET页面之间传递值的几种方式
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...
- Python爬虫_Selenium与PhantomJS
Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动化操作,不同是Selenium可以直接运 ...
- Java Eclipse和MyEclipse快捷键
摘自:http://www.cnblogs.com/lsy131479/p/8487379.html 首先: 常用快捷键 alt+/ - - 万能快捷键 Ctrl+1 - - 快速修复 Eclip ...
- opencv3.1线性可分svm例子及函数分析
https://www.cnblogs.com/qinguoyi/p/7272218.html //摘自:http://docs.opencv.org/2.4/doc/tutorials/ml/int ...
- php+js 防止被抓包篡改数据,数据签名校验
签名密钥,这个是自己生成的,需要客户端+服务端一致. <?php /** * 获取签名 * @param $data 提交的数据 * @param $key 安全密钥 * @return boo ...
- HTMLCollection 对象和NodeList 对象
获取html元素有三种方法,其中通过类名和标签获取的结果为一个HTMLCollection对象. HTMLCollection对象可以理解为一个包含html元素的数组(但不是数组),可以通过索引[ ] ...
- js输出
JavaScript 可以通过4种不同的方式在html页面输出数据 1.使用window.alert() 弹出警告框,由于window为js的内置类,可简写为alter() <script> ...
- 面试题:JS中map的陷阱
题目: ['2', '3', '4'].map(parseInt); 请说出上面代码的执行结果 错误回答: [2, 3, 4] 真正答案: [2, NaN, NaN] 解析: 因为 map 的算子是有 ...
- gdb远程debug A syntax error in expression, near `variable)'.
今天调试有个linux环境的应用时,gdb提示A syntax error in expression, near `variable)'.,最后经查,gdb版本过低(比如7.2)或者源代码不匹配所致 ...
- springboot 事务回滚
在springboot中,使用事务回滚时,添加@Transactional注解,然后在try-catch块中,发生异常时,在catch中 添加 TransactionAspectSupport.cur ...