hdu 4597 Play Game

Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 630    Accepted Submission(s): 374


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
 
DP: dp[al][ar][bl][br] 表示如果你是第一个挑,从第一堆a卡剩余标号为第al到ar的卡和从第一堆b卡剩余标号为第bl到br的卡挑能得到的最大分数。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int N = 22;
 7 int _, n, dp[N][N][N][N];
 8 int a[N], b[N], suma[N], sumb[N];
 9 
10 int DP(int al, int ar, int bl, int br, int flag)//flag为1表示先挑, flag为0表示后挑
11 {
12     if(al>ar&&bl>br) return 0;
13     if(dp[al][ar][bl][br]){
14         if(flag) return dp[al][ar][bl][br];
15         else return suma[ar] - suma[al-1] + sumb[br] - sumb[bl-1]-dp[al][ar][bl][br];
16     }
17     int ans = 0;
18     if(al<=ar){
19         ans = max(DP(al+1, ar, bl, br, 0) + a[al], ans);
20         ans = max(DP(al, ar-1, bl, br, 0) + a[ar], ans);
21     }
22     if(bl<=br){
23         ans = max(DP(al, ar, bl+1, br, 0) + b[bl], ans);
24         ans = max(DP(al, ar, bl, br-1, 0) + b[br], ans);
25     }
26     dp[al][ar][bl][br] = ans;
27     if(!flag) ans = suma[ar] - suma[al-1] + sumb[br] - sumb[bl-1] - ans;//如果是后挑,把剩余卡的综合减去先挑者能得到的最大分数
28     return ans;
29 }
30 
31 void solve()
32 {
33     memset(dp, 0, sizeof(dp));
34     scanf("%d", &n);
35     for(int i=1; i<=n; i++) {
36         scanf("%d", a+i); 
37         suma[i] = suma[i-1] + a[i];
38     }
39     for(int i=1; i<=n; i++) {
40         scanf("%d", b+i); 
41         sumb[i]= sumb[i-1] + b[i];
42     }
43     printf("%d
", DP(1, n, 1, n, 1));
44 }
45 
46 int main()
47 {
48 //    freopen("in.txt", "r", stdin);
49     scanf("%d", &_);
50     while(_--) solve();
51     return 0;
52 }
Play Game
原文地址:https://www.cnblogs.com/zyx1314/p/3843834.html