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 a i (1≤a i≤10000). The third line contains N integer b i (1≤b i≤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\n", 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值