Play GameTime
limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 880 Accepted Submission (s): 514
Problem Descriptionalice and Bob are playing a game. There is piles of cards. There is 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 would be added to his total s Core. Alice and Bob are both clever enough, and would pick up cards to get as many scores as possible. 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 a integer, indicating the most score Alice can get.
Sample Input
2 1 23 53
Sample Output
Links: http://acm.hdu.edu.cn/showproblem.php?pid=4597
Test instructions
Give you two stacks of cards, each time can be any pile of cards or the end of the card draw. Alice first smoked, Bob after the smoke, two people want to draw the most points of cards.
Practice:
Dp[az][ay][bz][by]. Az,ay on behalf of the first pile of cards to the left and to the right of the number of separate draw. Then in this state Bob pumped the points.
Because the DP represents the number of Bob, so the remaining odd cards in the stack, is Bob Pumping, to take the maximum value of the various pumping methods. If there are even cards left, then it is Alice Pumping, to take the minimum values of the various suction methods.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map>int dp[30][30][30][30];int a[30];int b[30];int dfs (int az , int ay,int bz,int by)//total number is even {if (dp[az][ay][bz][by]!=-1) return dp[az][ay][bz][by];int sum=ay-az+by-bz+2;int ans;if ( sum&1) ans=0;elseans=9999999;if (sum==0) return dp[0][0][0][0]=0;if (Az<=ay) {if (sum%2==1)//bob take {Ans=max (DFS ( Az+1,ay,bz,by) +a[az],ans); Ans=max (Dfs (Az,ay-1,bz,by) +a[ay],ans); }else{ans=min (Dfs (Az+1,ay,bz,by), ans); Ans=min (Dfs (Az,ay-1,bz,by), ans);}} if (bz<=by) {if (sum%2==1) {Ans=max (Dfs (Az,ay,bz+1,by) +b[bz],ans); Ans=max (Dfs (az,ay,bz,by-1) +b[by],ans); }else{ans=min (Dfs (Az,ay,bz+1,by), ans); Ans=min (Dfs (az,ay,bz,by-1), ans); }}rEturn Dp[az][ay][bz][by]=ans;} int main () {int t;scanf ("%d", &t), while (t--) {int n;scanf ("%d", &n); memset (dp,-1,sizeof dp); a[0]=a[n+1]=b[0]=b [N+1]=0;int sum=0;for (int i=0;i<n;i++) {scanf ("%d", &a[i+1]); sum+=a[i+1];} for (int i=0;i<n;i++) {scanf ("%d", &b[i+1]); sum+=b[i+1];} printf ("%d\n", Sum-dfs (1,n,1,n));} return 0;}
HDU 4597 Play Game memory Search interval DP