HDU 6199gems gems gems (DP)
gems gems gems
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 895 Accepted Submission(s): 167
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
【题意】给一个序列,两个人玩游戏,A先手,第一次取一个或者两个数,然后轮流取,每次取得个数与前一个人相同或多一个,从左到右取,A希望差值越大,B希望差值越小,求最大差值。
【分析】
动规的思路考虑, dp[person][idx][k] 表示 person==1?Bob:Alice 从第 idx 个开始取,能取 k个使得结果最大(最小) 的最优答案。
首先考虑,不考虑取的人,第 k 次最多取 k+1 个宝石。故 (k+1)(k+2)2≤n ,即 k 最大取值在sqrt(n*2)+1 。
故 dp 的最大枚举状态为 2×20000×200 。
此题的最大最小值的上下界在 INT 范围内,使用长整型会 MLE 。由于 dp[person][idx][k] 最大只与 dp[!person][idx+k][k+1] 产生联系,可用滚动数组的方式将第二维缩小。还有一种二维 做法,因为A希望自己的-B的差值大,B希望A-自己的差值小,也就是自己的-A的差值大,也就是说,两个人都希望自己的分数-对方的 结果尽量大,那么第一维就可以删掉。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e4+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
ll qpow(int x,int qq){ll f=,p=x;while(qq){if(qq&)f=f*p%mod;p=1LL*p*p%mod;qq>>=;}}
int n,m,k;
int dp[][M+][M];
int sum[N];
int getSum(int l,int r){return sum[r]-sum[l-];}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
sum[]=;
met(dp,);
for(int i=,x;i<=n;i++){
scanf("%d",&x);
sum[i]=sum[i-]+x;
}
for(int pos=n;pos;--pos){
int maxk=int(sqrt(*pos)+);
int remain=n-pos+;
for(int k=;k<=min(remain,maxk);k++ ){
if(k==remain){
dp[][pos&M][k]=getSum(pos,n);
dp[][pos&M][k]=-getSum(pos,n);
}
else{
if(pos+k-+k<=n){
dp[][pos&M][k]=dp[][(pos+k)&M][k];
dp[][pos&M][k]=dp[][(pos+k)&M][k];
if(pos+k+k+-<=n){
dp[][pos&M][k]=min(dp[][pos&M][k],dp[][(pos+k)&M][k+]);
dp[][pos&M][k]=max(dp[][pos&M][k],dp[][(pos+k)&M][k+]);
} }
dp[][pos&M][k]+=getSum(pos,pos+k-);
dp[][pos&M][k]-=getSum(pos,pos+k-);
}
}
}
int tmp=;
if(n>=){
tmp=dp[][][];
if(n>=) tmp=max(tmp,dp[][][]);
}
printf("%d\n",tmp);
}
return ;
}
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e4+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
ll qpow(int x,int qq){ll f=,p=x;while(qq){if(qq&)f=f*p%mod;p=1LL*p*p%mod;qq>>=;}}
int n,m,k;
int dp[N][M];
int sum[N];
int getSum(int l,int r){return sum[r]-sum[l-];}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
sum[]=;
met(dp,);
for(int i=,x;i<=n;i++){
scanf("%d",&x);
sum[i]=sum[i-]+x;
}
for(int pos=n;pos;--pos){
int maxk=int( sqrt(*pos)+ );
int remain=n-pos+;
for(int k=;k<=min(remain,maxk);k++ ){
if(k==remain){
dp[pos][k]=getSum(pos,n);
}
else{
if(pos+k-+k<=n){
dp[pos][k]=dp[pos+k][k];
if(pos+k+k+-<=n){
dp[pos][k]=max(dp[pos][k],dp[pos+k][k+]);
}
}
dp[pos][k]=getSum(pos,pos+k-)-dp[pos][k];
}
}
}
int tmp=;
if(n>=){
tmp=dp[][];
if(n>=) tmp=max(tmp,dp[][]);
}
printf("%d\n",tmp);
}
return ;
}
HDU 6199gems gems gems (DP)的更多相关文章
- HDU 5791:Two(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description Alice gets two sequences A ...
- HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))
Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...
- HDU 4833 Best Financing (DP)
Best Financing Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 1422 重温世界杯(DP)
点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...
- HDU 1176 免费馅饼(DP)
点我看题目 题意 : 中文题.在直线上接馅饼,能接的最多是多少. 思路 :这个题其实以前做过.....你将这个接馅饼看成一个矩阵,也不能说是一个矩阵,反正就是一个行列俱全的形状,然后秒当行,坐标当列, ...
- hdu 4055 Number String(dp)
Problem Description The signature of a permutation is a string that is computed as follows: for each ...
- 【HDU - 4345 】Permutation(DP)
BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...
- HDU 5375 Gray code(DP)
题意:给一串字符串,里面可能出现0,1,?,当中问号可能为0或1,将这个二进制转换为格雷码后,格雷码的每位有一个权值,当格雷码位取1时.加上该位权值,求最大权值和为多少. 分析:比赛的时候愚了.竟然以 ...
- hdu 1158 Employment Planning(DP)
题意: 有一个工程需要N个月才能完成.(n<=12) 给出雇佣一个工人的费用.每个工人每个月的工资.解雇一个工人的费用. 然后给出N个月所需的最少工人人数. 问完成这个项目最少需要花多少钱. 思 ...
- hdu 2189 来生一起走(DP)
题意: 有N个志愿者.指挥部需要将他们分成若干组,但要求每个组的人数必须为素数.问不同的方案总共有多少.(N个志愿者无差别,即每个组的惟一标识是:人数) 思路: 假设N个人可分为K组,将这K组的人数从 ...
随机推荐
- linux 下 mysql 主从配置
话不多说,直接干. 准备条件:安装两个mysql数据库,随便哪个作主库,另一个从库. 1.在主库创建 复制用的账号 grant replication slave ,replication clien ...
- javascript操作对象的方法
with 确定某个对象的作用区域,在with代码段内的次对象的属性或方法可以直接使用. 例: //比如stu中有name,age属性和walk方法 with(stu) { alert(name+&qu ...
- js获得页面鼠标位置
1.客户区坐标位置:clientX,clientY 鼠标相对于在当前页面可视范围左上角的位置 2.页面坐标位置:pageX,pageY 鼠标相对于页面左上角的位置(受滑动等影响,例如pageY=cli ...
- Metasploit 一些重要模块使用介绍
本文是"T00LS Metasploit(第一季)"的文档版,是个人在观看视频动手操作的一个记录,仅供学习.文中会介绍Metasploit的一些基本使用:端口扫描.smb扫描.服务 ...
- PHP深浅拷贝
举个栗子: <?php class Example1 { public $name; public function __construct($name) { $this->name = ...
- flask插件系列之flask_caching缓存
前言 为了尽量减少缓存穿透,同时减少web的响应时间,我们可以针对那些需要一定时间才能获取结果的函数和那些不需要频繁更新的视图函数提供缓存服务,可以在一定的时间内直接返回结果而不是每次都需要计算或者从 ...
- (转)什么是CDC类(Communication Device Class)
全文地址:http://justmei.blog.163.com/blog/static/1160998532010321112522467/ 什么是CDC类 (Communication Devic ...
- BZOJ 3958 Mummy Madness
Problem BZOJ Solution 算法:二分+扫描线 快要2019年了,就瞎写一篇博客来凑数,不然感觉太荒凉了-- 答案是可二分的,那么二分的依据是什么呢?不妨设当前二分的答案为\(mid\ ...
- Ubuntu每次开机后提示:检测到系统程序出现问题的解决方法
首先,错误报告存放位置: cd /var/crash/ ls //可以查看错误报告 1 2 sudo rm /var/crash/* //删除该目录下的所有文件 1 但是,这只是删除掉的是错误报告,如 ...
- 5.Python3标准库-日期和时间
''' 不同于int,str,float,Python没有包含对应日期和时间的原生类型,不过提供了3个相应的模块,可以采用多种表示来管理日期和时间值 time模块由底层C库提供与时间相关的函数.它包含 ...