http://acm.hdu.edu.cn/showproblem.php?pid=4597
dp[al][ar][bl][br] 表示先手拿到最高分。这样有四种选择去转移。以拿 a[al] 为例子。
剩下的部分里面对手会拿 dp[al + 1][ar][bl][br] 这么多,那么你还可以拿 sum - dp。总共可以拿 a[al] + sum - dp。
难度不高。
#include <bits/stdc++.h>
const int N = 22;
int dp[N][N][N][N];
int suma[N];
int sumb[N];
int a[N];
int b[N];
int dfs(int, int, int, int);
int calc(int al, int ar, int bl, int br)
{
int sum = suma[ar] - suma[al - 1] + sumb[br] - sumb[bl - 1];
return sum - dfs(al, ar, bl, br);
}
int dfs(int al, int ar, int bl, int br)
{
int& now = dp[al][ar][bl][br];
if (now != -1)
return now;
now = 0;
if (ar >= al)
{
now = std::max(now, a[al] + calc(al + 1, ar, bl, br));
now = std::max(now, a[ar] + calc(al, ar - 1, bl, br));
}
if (br >= bl)
{
now = std::max(now, calc(al, ar, bl + 1, br) + b[bl]);
now = std::max(now, calc(al, ar, bl, br - 1) + b[br]);
}
return now;
}
int main()
{
int casc;
scanf("%d", &casc);
for (int casi = 1; casi <= casc; casi++)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", a + i);
for (int i = 1; i <= n; i++)
scanf("%d", b + i);
for (int i = 1; i <= n; i++)
{
suma[i] = suma[i - 1] + a[i];
sumb[i] = sumb[i - 1] + b[i];
}
memset(dp, -1, sizeof(dp));
printf("%d\n", dfs(1, n, 1, n));
}
return 0;
}