Play game
Time Limit: 2000/1000 MS (Java/others) memory limit: 65535/65535 K (Java/Others)
Total submission (s): 54 accepted submission (s): 36
Problem descriptionalice 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 your scores as possible. do you know how many scores can Alice get if he picks up first?
Inputthe 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 ).
Outputfor 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
Source2013 ACM-ICPC Jilin Tonghua national invitational Competition -- reproduction of questions
Recommendliuyiding is an interval DP, because each fetch can take the header and tail of two queues, so it is a two-dimensional interval DP, we use DP [Al] [ar] [BL] [br] to represent the first series from Al to AR, and the second series from BL to BR, the current person has the maximum score of the first choice, so we can take the heads and tails of two queues, there are four state transition equations! You can use a memory-based search!
#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;#define MAXN 26int suma[MAXN],sumb[MAXN],pa[MAXN],pb[MAXN],dp[MAXN][MAXN][MAXN][MAXN];int dfs(int al,int ar,int bl ,int br){ if(dp[al][ar][bl][br]!=-1) return dp[al][ar][bl][br]; dp[al][ar][bl][br]=0; if(al<=ar) dp[al][ar][bl][br]=suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al+1,ar,bl,br); if(al<=ar) dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar-1,bl,br)); if(bl<=br) dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl+1,br)); if(bl<=br) dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl,br-1)); return dp[al][ar][bl][br];}int main (){ int n,i,tcase; scanf("%d",&tcase); while(tcase--) { scanf("%d",&n); suma[0]=sumb[0]=0; for(i=1;i<=n;i++) { scanf("%d",&pa[i]); suma[i]=suma[i-1]+pa[i]; } for(i=1;i<=n;i++) { scanf("%d",&pb[i]); sumb[i]=sumb[i-1]+pb[i]; } memset(dp,-1,sizeof(dp)); printf("%d\n",dfs(1,n,1,n)); } return 0;}