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组的人数从 ...
随机推荐
- 使用HttpClient4来构建Spring RestTemplate
Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...
- Elasticsearch——QueryBuilder简单查询
elasticsearch中存储的全部文档 1.matchAllQuery() matchAllQuery()方法用来匹配全部文档 public class QueryTest { pub ...
- 51nod1773 A国的贸易
基准时间限制:2 秒 空间限制:524288 KB 分值: 40 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们 ...
- jQuery.Event的一些用法
直接写用法 //创建一个事件 var event = $.Event("事件类型",["定义的事件参数最终将出现在e1中"]); //绑定一个处理器 $(obj ...
- 探讨一个“无法创建JVM”的问题(已解决)
ava版本:1.4 运行设置: -Xms1G -Xmx4G 报错: [ Incompatible initial and maximum heap sizes specified: ][ initia ...
- HBA 介绍
1.首先介绍一下什么是HBA. 这里所说的HBA,全称FC HBA,也就是Fibre Channel Host Bus Adapter.在FC网络中,主机(如服务器)需要和FC网络.FC存储设备(如S ...
- Fiddler -工具使用介绍(附:拦截请求并修改返回数据)(转)
一.Fiddler 介绍 Fiddler 是一个使用 C# 编写的 http 抓包工具.它使用灵活,功能强大,支持众多的 http 调试任务,是 web.移动应用的开发调试利器. 1,功能特点 同 H ...
- PHP对象3: public / private / protected
<?php /* public 可继承, 内外可访问 private 不可, 只内部访问 protected 可继承, 只内部 */ class A{ protected $name; priv ...
- struct msghdr和struct cmsghdr【转载】
理解struct msghdr当我第一次看到他时,他看上去似乎是一个需要创建的巨大的结构.但是不要怕.其结构定义如下:struct msghdr { void *msg_name ...
- selenium只打开一个浏览器窗口
from selenium.webdriver import Remote from selenium.webdriver.chrome import options from selenium.co ...