Game
题目背景:
分析:DP
啊啦,十来天前的T2的加强版啊,首先,我们可以发现,k的最大值大概就是在sqrt(2 * n)左右,也就是只有200左右,那么事情就简单了,我们定义dp[i][j]表示已经取完了最后i个,最后一次取的j个,那么就可以从dp[i - j][j]或者dp[i - j][j + 1]转移过来,显然对手也很聪明,一定会从两者之间选择较大的一个,所以dp[i][j] = sum[i] - sum[i - j] - max(dp[i - j][j], dp[i - j][j + 1])(sum[i]为后缀和)实际实现的过程中可以直接把数组倒过来就好了。
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
const int MAXN = 20000 + 10;
const int MAXK = 200 + 20;
int n, t;
int a[MAXN], sum[MAXN];
int dp[MAXN][MAXK];
inline void solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
std::reverse(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + a[i];
int k = sqrt(n * 2);
while ((1 + k) * k <= n * 2) ++k;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j) {
if (j > i) dp[i][j] = 0;
else {
int pos = i - j;
dp[i][j] = sum[i] - sum[pos] -
std::max(dp[pos][j], dp[pos][j + 1]);
}
}
std::cout << std::max(dp[n][1], dp[n][2]) << '\n';
}
int main() {
// freopen("game.in", "r", stdin);
// freopen("game.out", "w", stdout);
scanf("%d", &t);
while (t--) solve();
return 0;
}