Problem
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.
Limit
1≤n≤20000
−100000≤Vi≤100000
Idea
此题甚是熟悉,但忘了出处在哪…
动规的思路考虑, dp[person][idx][k]
表示 person==1?Bob:Alice
从第 idx 个开始取,能取 k 或 k+1 使得结果最大(最小) 的最优答案。
首先考虑,不考虑取的人,第 k 次最多取 k+1 个宝石。故 (k+1)(k+2)2≤n ,即 k 最大取值在 n∗2−−−−√≈200 。
故 dp
的最大枚举状态为
2×20000×200
。大致满足时限要求(试了比较多次,记忆化无法通过。但不排除某些操作可行)。
具体转移详见代码。
此题的最大最小值的上下界在 INT 范围内,使用长整型会 MLE 。由于 dp[person][idx][k]
最大只与 dp[!person][idx+k+1][k+1]
产生联系,可用滚动数组的方式将第二维缩小。
Code
#include<bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int N = 20000 + 10;
const int K = 210;
const int TN = 255 + 10;
const int MOD = 255;
int T, n;
int v[N], pre[N], dp[2][TN][K];
int main()
{
scanf("%d", &T);
while(T-- && scanf("%d", &n)!=EOF)
{
memset(dp, 0, sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d", &v[i]);
pre[i] = pre[i-1] + v[i];
}
int lmt = sqrt(n*2.0) + 1;
for(int idx=n;idx;idx--)
for(int k=lmt;k;k--)
{
if(idx+k <= n) {
dp[0][idx&MOD][k] = pre[idx+k-1] - pre[idx-1] +
max(dp[1][(idx+k)&MOD][k], dp[1][(idx+k+1)&MOD][k+1] + v[idx+k]);
dp[1][idx&MOD][k] = -pre[idx+k-1] + pre[idx-1] +
min(dp[0][(idx+k)&MOD][k], dp[0][(idx+k+1)&MOD][k+1] - v[idx+k]);
}
else if(idx+k-1 <= n) {
dp[0][idx&MOD][k] = dp[1][(idx+k)&MOD][k] + pre[idx+k-1] - pre[idx-1];
dp[1][idx&MOD][k] = dp[0][(idx+k)&MOD][k] - pre[idx+k-1] + pre[idx-1];
}
}
printf("%d\n", dp[0][1][1]);
}
}