HDU 4597

题目大意:

两人轮流从两堆牌从抽取最顶端或者最底部的牌,得到的分数加到自己身上,问先拿牌的最多能得多少分

记忆化搜索,2堆牌的底和顶,有四种方法,根据四种方法来找到最优解

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 int dp[22][22][22][22],p[22],q[22];
 6 int dfs(int a,int b,int c,int d,int sum)
 7 {
 8     int maxn=0;
 9     if(a>b&&c>d) return 0;
10     if(dp[a][b][c][d]) return dp[a][b][c][d];
11     if(a<=b){
12         maxn=max(maxn,sum-dfs(a+1,b,c,d,sum-p[a]));
13         maxn=max(maxn,sum-dfs(a,b-1,c,d,sum-p[b]));
14     }
15     if(c<=d){
16         maxn=max(maxn,sum-dfs(a,b,c+1,d,sum-q[c]));
17         maxn=max(maxn,sum-dfs(a,b,c,d-1,sum-q[d]));
18     }
19     dp[a][b][c][d]=maxn;
20     return maxn;
21 }
22 int main()
23 {
24     int T,n,sum;
25     scanf("%d",&T);
26     while(T--){
27         sum=0;
28         scanf("%d",&n);
29         for(int i=1;i<=n;i++){
30             scanf("%d",&p[i]);
31             sum+=p[i];
32         }
33         for(int i=1;i<=n;i++){
34             scanf("%d",&q[i]);
35             sum+=q[i];
36         }
37         memset(dp,0,sizeof(dp));
38         int ans=dfs(1,n,1,n,sum);
39         printf("%d\n",ans);
40     }
41     return 0;
42 }
时间: 2024-10-07 06:34:07

HDU 4597的相关文章

HDU 4597 Play Game (记忆化搜索)

题意:有两堆n张的卡片,每张卡片有一个得分,Alice和Bob轮流在两堆卡片的两端取卡片 问Alice先手,取得分数最多为多少: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

hdu 4597 Play Game(记忆化搜索)

题目链接:hdu 4597 Play Game 题目大意:给出两堆牌,仅仅能从最上和最下取,然后两个人轮流取,都依照自己最优的策略.问说第一个人对多的分值. 解题思路:记忆化搜索,状态出来就很水,dp[fl][fr][sl][sr][flag],表示第一堆牌上边取到fl,以下取到fr,相同sl.sr为第二堆牌,flag为第几个人在取.假设是第一个人,dp既要尽量大,假设是第二个人,那么肯定尽量小. #include <cstdio> #include <cstring> #incl

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

HDU 4597 Play Game(记忆化搜索,深搜)

题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的分值.//解题思路:记忆化搜索,状态出来就非常水,dp[fl][fr][sl][sr][flag],//表示第一堆牌上边取到fl,下面取到fr,同样sl,sr为第二堆牌,flag为第几个人在取.//如果是第一个人,dp既要尽量大,如果是第二个人,那么肯定尽量小. http://www.2cto.co

HDU 4597(记忆化搜索 dfs 参考)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 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

hdu 4597 Play Game 区间dp

Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4597 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 tur

hdu 4597 Play Game (记忆化搜索 区间dp)

#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int dp[30][30][30][30]; int vis[30][30][30][30]; int a[2][30],sum[2][30]; int dfs(int i,int j,int k,int l) { if(vi

HDU 4597 Play Game(记忆化)

Play Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 894    Accepted Submission(s): 525 Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card

hdu 4597 Play Game(区间dp,记忆化搜索)

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