HDU 4597 Play Game 记忆化DP

Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

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
2

3
5
3
3
10 100
20
2 4 3
 
Sample Output
53
105
 
Source
 
 
   没有后效性,因为每一状态都是最优策略。
   确定状态的转移 。每个状态的数的和一定,一旦子问题取数和确定,则父节点取树和确定,枚举4中情况,取最大(优)。
   此题递归树性质:
          父亲节点father 的取数和 与  儿子节点son的取数和 为 定值 。这就是状态转移的关键地方 。 
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
int a[] ,b[] ;
int suma[] ,sumb[] ;
int dp[][][][] ;
int N ; int dfs(int U , int B ,int L ,int R){
if(dp[U][B][L][R] != -)
return dp[U][B][L][R] ;
if(U > B && L > R)
return dp[U][B][L][R] = ;
int sum = ;
if(U <= B)
sum += suma[B] - suma[U-] ;
if(L <= R)
sum += sumb[R] - sumb[L-] ;
int nowstate = ;
if(U == B)
nowstate = Max(nowstate , sum - dfs(U+,B-,L,R)) ;
else if(U < B){
nowstate = Max(nowstate , sum - dfs(U+,B,L,R)) ;
nowstate = Max(nowstate , sum - dfs(U,B-,L,R)) ;
}
if(L == R)
nowstate = Max(nowstate , sum - dfs(U,B,L+,R-)) ;
else if(L < R){
nowstate = Max(nowstate , sum - dfs(U,B,L+,R)) ;
nowstate = Max(nowstate , sum - dfs(U,B,L,R-)) ;
}
return dp[U][B][L][R] = nowstate ;
} int main(){
int T ;
scanf("%d",&T) ;
while(T--){
scanf("%d",&N) ;
suma[] = sumb[] = ;
for(int i = ; i <= N ; i++){
scanf("%d",&a[i]) ;
suma[i] = suma[i-] + a[i] ;
}
for(int i = ; i <= N ; i++){
scanf("%d",&b[i]) ;
sumb[i] = sumb[i-] + b[i] ;
}
memset(dp,-,sizeof(dp)) ;
printf("%d\n",dfs(,N,,N)) ;
}
return ;
}
 
上一篇:JavaSe:UncaughtExceptionHandler


下一篇:NLog 自定义Target