Reprint Please specify Source: http://blog.csdn.net/u012860063Topic Link: http://acm.hdu.edu.cn/showproblem.php?
pid=4597 Test Instructions Alice and Bob play a game that has a sequence of two positive integer numbers of length n, each time they two
Just select one of the two ends from one of the sequences to take away. They all want to get as big as possible.
The sum of the numbers, and they are smart enough. Select the optimal strategy every time. Alice first chooses, asks
What is the sum of the numbers Alice has finally got?
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
Source2013 ACM-ICPC Jilin Tonghua National Invitational--title reappearance
The code is as follows:
#include <cstdio> #include <cstring> #define MAX 20+10int S1[max], S2[max], Sum1[max], Sum2[max];int dp[max][ MAX][MAX][MAX];//DP[A][B][I][J] represents the maximum value that the current player can obtain from the i~j of S1 A~B,S2 int max (int a, int b) {if (a > B) return A;return B;} int dfs (int a, int b, int i, int j) {if (Dp[a][b][i][j]) return dp[a][b][i][j];if (a > B && i > J) return 0;int m Ax1 = 0;int Max2 = 0;if (a <= b) Max1=max (S1[a]+dfs (a+1,b,i,j), S1[b]+dfs (A,B-1,I,J));//if (i <= j) Max2=max before and after the median value (s2 [I]+dfs (A,b,i+1,j), S2[j]+dfs (a,b,i,j-1)), or//Take a large dp[a][b][i][j]=sum1[b]-sum1[a-1]+sum2[j]-sum2[i-1]-max (MAX1, MAX2);//interval and minus the remainder of the opponent is the return of the current player dp[a][b][i][j];} int main () {int T, N;while (~SCANF ("%d", &t)) {while (t--) {memset (dp,0,sizeof (DP)); memset (Sum1,0,sizeof (sum1)); memset (sum2,0,sizeof (sum2)); int ans = 0;int I, j;scanf ("%d", &n), for (i = 1; I <= n; i++) {scanf ("%d", &s1[i]); sum 1[i] = Sum1[i-1]+s1[i];} for (i = 1; I <= n; i++) {scanf ("%d", &s2[i]); Sum2[i] = Sum2[i-1]+s2[i];} Ans = Sum1[n]+sum2[n]-dfs (1,n,1,n);p rintf ("%d\n", ans);}} return 0;}
hdu4597 Play Game (DFS)