HDU 6199 gems gems gems dp
来源:互联网 发布:mpu6050单片机电路图 编辑:程序博客网 时间:2024/05/01 14:31
题目链接:HDU 6199
gems gems gems
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 979 Accepted Submission(s): 193
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 takek 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.
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
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 numbersn (1≤n≤20000 );
the second line contains n numbers:V1,V2…Vn . (−100000≤Vi≤100000 )
For each test case:
the first line contains a numbers
the second line contains n numbers:
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
131 3 2
Sample Output
4
题目分析:比赛时被博弈的思想套住了,既然找不到必胜必负态自然也无法找规律或者sg函数,实际上是dp的题,dp[i][j][k] 代表第i个人从第j个开始取,可取的是k或k+1。i取0或1,j取20000,k的最大值是sqrt(n*2)<=200,状态转移方程是dp[0][i][j]=区间和[i~i+j-1]+max(dp[1][i+j][j],dp[1][i+j+1][j+1]+s[i+j])对于另一个人也是一样,然后注意有时取不到k+1,之后网上get到的每次只会影响k区间范围的数,所以滚动数组即可,滚动的大小比k最大值大即可,这里取256,%256可以用&255来优化。
//// main.cpp// gems gems gems//// Created by teddywang on 2017/09/10.// Copyright © 2017年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int T;int dp[2][300][300];int n;int s[21001],pre[21001];int mod=255;int main(){ scanf("%d",&T); while(T--) { scanf("%d",&n); memset(dp,0,sizeof(dp)); pre[0]=0; for(int i=1;i<=n;i++) { scanf("%d",&s[i]); pre[i]=pre[i-1]+s[i]; } int size=sqrt(n*2)+1; for(int i=n;i>=1;i--) { for(int j=size;j>=1;j--) { if(i+j<=n) { dp[0][i&mod][j]=pre[(i+j-1)]-pre[i-1]+max(dp[1][(i+j)&mod][j],dp[1][(i+j+1)&mod][j+1]+s[i+j]); dp[1][i&mod][j]=-pre[i+j-1]+pre[i-1]+min(dp[0][(i+j)&mod][j],dp[0][(i+j+1)&mod][j+1]-s[i+j]); } else if(i+j==n+1) { dp[0][i&mod][j]=pre[i+j-1]-pre[i-1]+dp[1][(i+j)&mod][j]; dp[1][i&mod][j]=pre[i-1]-pre[i+j-1]+dp[0][(i+j)&mod][j]; } } } printf("%d\n",dp[0][1][1]); } }
阅读全文
0 0
- HDU 6199 gems gems gems dp
- hdu 6199 gems gems gems dp
- hdu 6199 gems gems gems(DP)
- HDU 6199 gems gems gems
- hdu 6199 gems gems gems
- HDU 6199 gems gems gems [DP+DP优化]
- HDU 6199 gems gems gems dp(逆推)
- HDU 6199 gems gems gems (DP, 2017 ACM/ICPC Asia Regional Shenyang Online)
- HDU 6199 gems gems gems 2017沈阳网络赛 1006
- HDU 6199 gems gems gems (2017沈阳网络赛)
- 2017沈阳网络赛 HDU 6199 gems gems gems
- HDU 6199 gems gems gems (2017沈阳网赛
- HDU 4778 Gems Fight!(dp)
- 【树形DP】 珠宝(GEMS)
- hdu-4778-Gems Fight!---状态压缩dp
- hdu 4778 Gems Fight!(状态压缩dp)
- HDU 4778 Gems Fight! 状态压缩dp
- [博弈dp] hdu 4778 Gems Fight!
- spfa算法
- 解决jsp上传文件,重启tomcat后文件和文件夹自动删除
- CodeForces 723C Polycarp at the Radio (贪心+暴力)
- think in java第十六章数组 总结随笔
- diff.js 列表对比算法 源码分析
- HDU 6199 gems gems gems dp
- org.mybatis.spring.MyBatisSystemException
- python3 如何实现python2 中cmp()函数的功能
- 关于intern和字符串
- Python--脚本自动发送邮件
- java容器,java框架
- Linux shell 处理文件是,注意换行符要换成Unix格式
- Linux学习笔记——Makefile教程
- ubuntu网络设置