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

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

【题意】给一个序列,两个人玩游戏,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)的更多相关文章

  1. HDU 5791:Two(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 Two Problem Description   Alice gets two sequences A ...

  2. HDU 4833 Best Financing(DP)(2014年百度之星程序设计大赛 - 初赛(第二轮))

    Problem Description 小A想通过合理投资银行理财产品达到收益最大化.已知小A在未来一段时间中的收入情况,描述为两个长度为n的整数数组dates和earnings,表示在第dates[ ...

  3. HDU 4833 Best Financing (DP)

    Best Financing Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 1422 重温世界杯(DP)

    点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...

  5. HDU 1176 免费馅饼(DP)

    点我看题目 题意 : 中文题.在直线上接馅饼,能接的最多是多少. 思路 :这个题其实以前做过.....你将这个接馅饼看成一个矩阵,也不能说是一个矩阵,反正就是一个行列俱全的形状,然后秒当行,坐标当列, ...

  6. hdu 4055 Number String(dp)

    Problem Description The signature of a permutation is a string that is computed as follows: for each ...

  7. 【HDU - 4345 】Permutation(DP)

    BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...

  8. HDU 5375 Gray code(DP)

    题意:给一串字符串,里面可能出现0,1,?,当中问号可能为0或1,将这个二进制转换为格雷码后,格雷码的每位有一个权值,当格雷码位取1时.加上该位权值,求最大权值和为多少. 分析:比赛的时候愚了.竟然以 ...

  9. hdu 1158 Employment Planning(DP)

    题意: 有一个工程需要N个月才能完成.(n<=12) 给出雇佣一个工人的费用.每个工人每个月的工资.解雇一个工人的费用. 然后给出N个月所需的最少工人人数. 问完成这个项目最少需要花多少钱. 思 ...

  10. hdu 2189 来生一起走(DP)

    题意: 有N个志愿者.指挥部需要将他们分成若干组,但要求每个组的人数必须为素数.问不同的方案总共有多少.(N个志愿者无差别,即每个组的惟一标识是:人数) 思路: 假设N个人可分为K组,将这K组的人数从 ...

随机推荐

  1. 解决CodeBlocks无法自动补全的问题

    在Deepin下安装的CB,输入printf.scanf的时候不会自动补全,这样就很难受. 解决办法是在Setting -> Editor -> Syntax highlighting - ...

  2. HDU 5700 优先队列(或者multiset) 或 线段树

    题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即 ...

  3. CF835 C 前缀和

    100*100规模上第一象限坐标系上有1e5规模的点,每个点随时间在同一个值域内(最大10)周期递增,但初始值不同,给出一个矩阵和时间询问此时范围内点的值的和. 预处理初始时刻不同权值下的二维前缀和, ...

  4. JavaScript入门笔记(一)

    JavaScipt 2.1 javascript的组成部分 ECMAScript: 它是整个 javascript 的核心,包含(基本语法.变量.关键字.保留字.数据类型.语句.函数等等)DOM:文档 ...

  5. Python进行数据分析(二)MovieLens 1M 数据集

    # -*- coding: utf-8 -*- """ Created on Thu Sep 21 12:24:37 2017 @author: Douzi " ...

  6. Python学习笔记(三十四)—内置模块(3)base64

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431954588 ...

  7. PHP扩展--XHProf优化PHP程序

    简介 XHProf 是一个轻量级的分层性能测量分析器. 在数据收集阶段,它跟踪调用次数与测量数据,展示程序动态调用的弧线图. 它在报告.后期处理阶段计算了独占的性能度量,例如运行经过的时间.CPU 计 ...

  8. Uploadify & jQuery.imgAreaSelect 插件实现图片上传裁剪

    在网站中需要一个图片上传裁剪的功能,借鉴这篇文章 Ajax+PHP+jQuery图片截图上传 的指点,找到了jquery.imgAreaSelect这个不错插件,能对图片进行自定义区域选择并给出坐标, ...

  9. [php]apache虚拟主机配置

    1.所谓虚拟主机的配置,即url与磁盘目录的绑定 2.在httpd.conf中查询Virtual host,发现有注释说明需要在conf/extra/httpd-vhosts.conf中进行配置. 3 ...

  10. 【LibreOJ】#6299. 「CodePlus 2018 3 月赛」白金元首与克劳德斯

    [题意]给出坐标系中n个矩形,类型1的矩形每单位时间向x轴正方向移动1个单位,类型2的矩形向y轴正方向,初始矩形不重叠,一个点被矩形覆盖当且仅当它在矩形内部(不含边界),求$(-\infty ,+\i ...