hdu 4597 Play Game
Play Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 317 Accepted Submission(s): 196
Problem 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 ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤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
Source
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
Recommend
liuyiding
1 //78MS 1752K 1143 B C++ 2 /* 3 4 题意: 5 两排数,每个人轮流从任意一排的头或尾取一个, 6 求先手能取到的和的最大值 7 8 区间DP: 9 dp[l1][r1][l2][r2]表示在第一排的l1~r1和第二排的 10 l2~r2内先手能取到的和的最大值 11 使用区间DP的思想做记忆化搜索 12 13 */ 14 #include<stdio.h> 15 #include<string.h> 16 int dp[25][25][25][25]; 17 int suma[25],sumb[25]; 18 int Max(int a,int b) 19 { 20 return a>b?a:b; 21 } 22 int dfs(int l1,int r1,int l2,int r2) 23 { 24 if(dp[l1][r1][l2][r2]!=-1) 25 return dp[l1][r1][l2][r2]; 26 if(l1>r1 && l2>r2) 27 return dp[l1][r1][l2][r2]=0; 28 int ans=0; 29 int sum=0; 30 if(l1<=r1){ 31 sum+=suma[r1]-suma[l1-1]; 32 } 33 if(l2<=r2){ 34 sum+=sumb[r2]-sumb[l2-1]; 35 } 36 if(l1<=r1){ 37 ans=Max(ans,sum-dfs(l1+1,r1,l2,r2));//深搜遍历子区间 38 ans=Max(ans,sum-dfs(l1,r1-1,l2,r2)); 39 } 40 if(l2<=r2){ 41 ans=Max(ans,sum-dfs(l1,r1,l2+1,r2)); 42 ans=Max(ans,sum-dfs(l1,r1,l2,r2-1)); 43 } 44 return dp[l1][r1][l2][r2]=ans; 45 } 46 int main(void) 47 { 48 int t,n,a; 49 scanf("%d",&t); 50 while(t--) 51 { 52 memset(dp,-1,sizeof(dp)); 53 suma[0]=sumb[0]=0; 54 scanf("%d",&n); 55 for(int i=1;i<=n;i++){ 56 scanf("%d",&a);suma[i]=suma[i-1]+a; 57 } 58 for(int i=1;i<=n;i++){ 59 scanf("%d",&a);sumb[i]=sumb[i-1]+a; 60 } 61 printf("%d\n",dfs(1,n,1,n)); 62 } 63 return 0; 64 }