【HDU6199 2017 ACM ICPC Asia Regional Shenyang Online F】【博弈 DP】gems gems gems 双人从左侧拿宝石 每次拿相同或加一的最小差值

原创 2017年09月13日 09:47:54

gems gems gems

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1172    Accepted Submission(s): 242


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 (1T10), the number of the test cases. 
For each test case:
the first line contains a numbers n (1n20000);
the second line contains n numbers: V1,V2Vn. (100000Vi100000)
 

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

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 2e4 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n;
int sum[N];
int v[N];
int f[N][202];
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		for (int i = n; i >= 1; --i)scanf("%d", &v[i]);
		for (int i = 1; i <= n; ++i)sum[i] = sum[i - 1] + v[i];

		//设m为最大能取到的数量,则有(1+m)*m<=n*2,得到m^2+m<=n*2,得到m<sqrt(n*2)
		int m = sqrt(n * 2);
		while ((1 + m) * m > n * 2)--m;

		for (int i = 1; i <= n; ++i)//枚举当前还剩下几个
		{
			int top = min(i, m);
			for (int j = 1; j <= top; ++j)//枚举上一轮的人拿了多少个
			{
				f[i][j] = -2e9;
				gmax(f[i][j], (sum[i] - sum[i - j]) - f[i - j][j]);							//这一轮拿一样多
				if(i > j)gmax(f[i][j], (sum[i] - sum[i - j - 1]) - f[i - j - 1][j + 1]);	//这一轮多拿一个
			}
		}
		printf("%d\n", f[n][1]);
	}
	return 0;
}
/*
【题意】
有n(2e4)个宝石
两个人轮流从左侧取宝石,Alice先手,首轮取1个或2个宝石,
如果上一轮取了k个宝石,则这一轮只能取k或k+1个宝石。
一旦不能再取宝石就结束。
双方都希望自己拿到的宝石数比对方尽可能多。
问你,先手比后手多拿的最大宝石数。

【分析】
我们用f[i][j]表示剩下i和宝石没取,上个人取了j个宝石,先手所能获得的最大宝石价值差。
则我们此时,如果可i>=j(或i>=j+1),则可以选择拿j或j+1个宝石,对应着f[i-k][k]的后继,有gmax(f[i][j], (sum[i]-sum[i-k]) - f[i-k][k])

1,所有非法状态,都以0为初始值即可
2,每个v最多使得绝对值偏差1e5,所以int即可保存所有状态

【时间复杂度&&优化】
O(nsqrt(n))

*/


版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^

相关文章推荐

HDU 6199 gems gems gems (DP, 2017 ACM/ICPC Asia Regional Shenyang Online)

ProblemNow there are n gems, each of which has its own value. Alice and Bob play a game with these n...

【2017沈阳网络赛】1006 hdu6199 gems gems gems 动态规划

Problem Description Now there are n gems, each of which has its own value. Alice and Bob play a...

HDU 6199 gems gems gems dp

题目链接:HDU 6199 gems gems gems Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/327...

HDU 6199 gems gems gems 2017沈阳网络赛 1006

看似博弈的DP,卡空间
  • ssSky_x
  • ssSky_x
  • 2017年09月10日 21:34
  • 371

hdu 6119 小小粉丝度度熊(尺取法)

合并相互交叉的区间,尺取法#include using namespace std;const long long MAXN = 100100; struct node { long lon...

hdu6199

沈阳网络赛1006 gems gems gems 题意是有n堆宝石(可能有负数),A和B从左到右拿宝石,A先手拿1或者2堆,假设某个人当前拿了k堆,那么下一个人只能拿k或者k+1堆,如果他取不了k...

HDU 6199 gems gems gems dp(逆推)

传送门:HDU 6199 题意:  两个人轮流拿物品,每一个物品都有一个价值,第 i 次拿 k 个那么 第 i + 1 次 可以拿 k 或者 k + 1 个。Alice想让价值差最大,Bob想让价值...

2017沈阳网络赛 HDU 6199 gems gems gems

题意 : 两个人轮流拿物品,每一个物品都有一个价值,第 i 次拿 k 个那么 第 i + 1 次 可以拿 k + 1 或者 k 个。每个人都采取最有策略问你最后两个人的差。 题解 : 首先我们发现每一...

【2017沈阳网络赛】1006 hdu6199 gems gems gems 动态规划

Problem Description Now there are n gems, each of which has its own value. Alice and Bob play a...

51Nod 1277 字符串中的最大值【Dp+set_next】

1277 字符串中的最大值 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个字符串的前缀是指包含该字...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【HDU6199 2017 ACM ICPC Asia Regional Shenyang Online F】【博弈 DP】gems gems gems 双人从左侧拿宝石 每次拿相同或加一的最小差值
举报原因:
原因补充:

(最多只允许输入30个字)