标签:while std txt freopen lse ems min main ret
博弈类DP题,设状态f[i][j][a][b]表示第一堆里的范围在i-j,第二堆的范围在a-b,Alice可以得到的最大值
则有:
dp[i][j][a][b] = max
{
dp[ i + 1 ] [ j ] [ a ] [ b ]
dp[ i ] [ i - 1 ] [ a ] [ b ]
dp[ i ] [ j ] [ a + 1 ] [ b ]
dp[ i ] [ j ] [ a ] [ b - 1 ]
}
code:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[22], b[22];
int f[22][22][22][22];
int sum1[22], sum2[22];
int dfs(int a1, int a2, int b1, int b2)
{
int &ans = f[a1][a2][b1][b2];
int now;
if (a1 > a2)
{
now = sum2[b2] - sum2[b1 - 1];
if (b1 == b2)
ans = now;
}
else if (b1 > b2)
{
now = sum1[a2] - sum1[a1 - 1];
if (a1 == a2)
ans = now;
}
else
{
now = sum1[a2] - sum1[a1 - 1] + sum2[b2] - sum2[b1 - 1];
}
if (ans != -1)
return ans;
ans = 0;
if (a1 <= a2)
{
ans = max(ans, now - min(dfs(a1 + 1, a2, b1, b2), dfs(a1, a2 - 1, b1, b2)));
}
if (b1 <= b2)
{
ans = max(ans, now - min(dfs(a1, a2, b1 + 1, b2), dfs(a1, a2, b1, b2 - 1)));
}
return ans;
}
int main()
{
// freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
sum1[i] = sum1[i - 1] + a[i];
}
for (int i = 1; i <= n; ++i)
{
scanf("%d", &b[i]);
sum2[i] = sum2[i - 1] + b[i];
}
memset(f, -1, sizeof(f));
cout << dfs(1, n, 1, n) << endl;
}
return 0;
}
标签:while std txt freopen lse ems min main ret
原文地址:https://www.cnblogs.com/wyctstf/p/11372420.html