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

原创 2015年05月09日 20:28:35

题目链接: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 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
 

Source

PS:

记忆化搜索。
将两个人抽象出来可以看做是一样的,因为他们都具备共同的性质:都很聪明(即在当前状态下保证是自己拿的最多)。
这样就好处理多了,假设dfs()能生成一个在当前状态下的最优解,用数组dp记录下来,然后递归完成子问题就形成记忆化搜索了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 32;
int a[maxn], b[maxn], suma[maxn], sumb[maxn];
int dp[maxn][maxn][maxn][maxn];
//dp[x][y][i][j]表示当前玩家从a堆的x~y,b堆的i~j能获得的最大价值 
int dfs(int x, int y, int i, int j)
{
    if(dp[x][y][i][j])
    {
        return dp[x][y][i][j];
    }
    if(x > y && i > j)
    {
        return 0;
    }
    int maxa = 0, maxb = 0;
    if(x <= y)
    {
        maxa = max(a[x]+dfs(x+1,y,i,j),a[y]+dfs(x,y-1,i,j));
    }
    if(i <= j)
    {
        maxb = max(b[i]+dfs(x,y,i+1,j),b[j]+dfs(x,y,i,j-1));
    }
    dp[x][y][i][j] = suma[y]-suma[x-1]+sumb[j]-sumb[i-1]-max(maxa,maxb);
    
    //区间和减去对手所取的剩下的就为当前玩家的
    return dp[x][y][i][j];
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(suma, 0,sizeof(suma));
        memset(sumb, 0,sizeof(sumb));
        int n;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
            suma[i] = suma[i-1]+a[i];
        }
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&b[i]);
            sumb[i] = sumb[i-1]+b[i];
        }
        int ans = suma[n]+sumb[n]-dfs(1,n,1,n);
        printf("%d\n",ans);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。vasttian

相关文章推荐

HDU 4597 Play Game 记忆化搜索

HDU 4597 Play Game  题目链接

HDU 4597 Play Game (记忆化搜索)无语的节奏

Play Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Tota...

HDU 4597 Play Game[博弈论 负值最大函数 记忆化搜索]

题干Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N c...

hdu 4597 dp+前缀和+记忆化搜索

dp[i][i+len1][j][j+len2] = sum[i][len1+i][j][j+len2] -min { dp[i+1][i+len1][j][j+len2] , dp[i][i+len...

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

题意: 给你两摞牌,每次可以任意一堆 的牌头或者牌尾抽牌。Alice先抽,Bob后抽,两个人都想抽到最多点数的牌。 做法: dp[az][ay][bz][by]。 az,ay代表第一堆牌左边 和右...

HDU 4597 Play Game(四维记忆化搜索)

做之前问虎哥比赛的时候怎么过掉的,虎哥说秒过。感觉自己好水啊。。。。 四维记忆化搜索。决策的条件是如果你剩下的数越小你得到的数越大。dp[l1][r1][l2][r2]代表当前状态的最优的情况。 ...

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

题目大意: 有两个人玩取数的游戏,每次只能从两堆数列的两头取一个数,问先取的那个人最大能够获得的数是多少。 解题思路: 有题解说是记忆化搜索,很显然,我用了4维dp保存结果。dp[i][j][k][t...

HDU 4597 Play Game (DP,记忆化搜索,博弈)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4597 大致题意:给出2堆数,Alice和Bob轮流拿,每次只能从每堆数的头或尾取,Alice先...

hdu1208Pascal's Travels (DFS+记忆化搜索(DP))

Problem Description An n x n game board is populated with integers, one nonnegative integer per squ...

HDU-1501 Zipper DFS+记忆化搜索

博客园社区首页新随笔联系管理订阅 随笔- 649  文章- 10  评论- 54  HDU-1501 Zipper DFS+记忆化搜索    ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)