好久没有做题了,水平已经完全在学弟之下了。
一个吉林邀请赛最水的题目。:(
其实这题一看到数据范围,只可以想到思路,直接爆搜,加个记忆化。
这题虽然A了,但是我还是没太想清楚一些边界情况,心虚着A了。
我的代码如下:
/*************************************************************************
> File Name: 4597.c
> Author: Stomach_ache
> Mail: 1179998621@qq.com
> Created Time: 2014年03月02日 星期日 13时04分27秒
> Propose:
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
int a[22], b[22], dp[22][22][22][22], n, t;
int sum_a[22], sum_b[22];
int
dfs(int f1, int e1, int f2, int e2) {
if (dp[f1][e1][f2][e2] != -1)
return dp[f1][e1][f2][e2];
if (f1 > e1 && f2 > e2)
return dp[f1][e1][f2][e2] = 0;
int res = 0, sum = 0;
if (f1 <= e1) sum += sum_a[e1] - sum_a[f1-1];
if (f2 <= e2) sum += sum_b[e2] - sum_b[f2-1];
if (f1 <= e1) {
res = max(res, sum - dfs(f1+1, e1, f2, e2));
res = max(res, sum - dfs(f1, e1-1, f2, e2));
}
if (f2 <= e2) {
res = max(res, sum - dfs(f1, e1, f2+1, e2));
res = max(res, sum - dfs(f1, e1, f2, e2-1));
}
return dp[f1][e1][f2][e2] = res;
}
int
main(void) {
scanf("%d", &t);
while ( t-- ) {
scanf("%d", &n);
int i;
sum_a[0] = sum_b[0] = 0;
for (i = 1; i <= n; i++) {
scanf("%d", a+i);
sum_a[i] = sum_a[i-1]+a[i];
}
for (i = 1; i <= n; i++) {
scanf("%d", b+i);
sum_b[i] = sum_b[i-1]+b[i];
}
memset(dp, -1, sizeof(dp));
printf("%d\n", dfs(1, n, 1, n));
}
return 0;
}