HDU 4597 H - Play Game

来源:互联网 发布:淘宝优衣库代购可信吗 编辑:程序博客网 时间:2024/04/28 12:25
H - Play Game
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4597

Description

Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?
 

Input

The first line contains an integer T (T≤100), indicating the number of cases. 
Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer a i (1≤a i≤10000). The third line contains N integer b i (1≤b i≤10000).
 

Output

For each case, output an integer, indicating the most score Alice can get.
 

Sample Input

2 1 23 53 3 10 100 20 2 4 3
 

Sample Output

53 105
 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define EXP 1e-8#define UP(i,x,e) for(i=x;i<e;i++)#define DOWN(i,x,e) for(i=x;i>e;i--)#define Max(a,b,c) max(a,max(b,c))#define Men(name,num) memset(name,num,sizeof(name))const int maxn=25;int T,N,a[maxn],b[maxn],dp[maxn][maxn][maxn][maxn];int DFS(int fs,int fe,int ss,int se,int sum) {    if(fs>fe&&ss>se)return 0;//当两个都达到了都没得选了,就返回零表示剩下的卡的值为零    if(dp[fs][fe][ss][se])return dp[fs][fe][ss][se];//已经存在,直接返回    int curmax=0;    if(fs<=fe) {        curmax=max(curmax,sum-DFS(fs+1,fe,ss,se,sum-a[fs]));//由于得到是当前最优解        curmax=max(curmax,sum-DFS(fs,fe-1,ss,se,sum-a[fe]));    }    if(ss<=se) {        curmax=max(curmax,sum-DFS(fs,fe,ss+1,se,sum-b[ss]));        curmax=max(curmax,sum-DFS(fs,fe,ss,se-1,sum-b[se]));    }    return dp[fs][fe][ss][se]=curmax;}int main() {#ifndef ONLINE_JUDGE    freopen("D://imput.txt","r",stdin);#endif // ONLINE_JUDGE    scanf("%d",&T);    while(T--) {        int sum=0;        scanf("%d",&N);        Men(dp,0);        for(int i=1; i<=N; i++) {            scanf("%d",&a[i]);            sum+=a[i];        }        for(int j=1; j<=N; j++) {            scanf("%d",&b[j]);            sum+=b[j];        }        printf("%d\n",DFS(1,N,1,N,sum));    }}

0 0