Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >HDU 4597 Play Game(DFS,区间DP)

HDU 4597 Play Game(DFS,区间DP)

作者头像
ShenduCC
发布于 2018-04-26 06:32:14
发布于 2018-04-26 06:32:14
60800
代码可运行
举报
文章被收录于专栏:算法修养算法修养
运行总次数:0
代码可运行

Play Game

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

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[x1][y1][x2][y2]表示当前先手的这个人在x1到y1,x2到y2,两堆的区间,可以获得最大值,dp[x1][y1][x2][y2]的值等于当前的和 sum-(下一个状态,即对方先手可以获得的最大值)。

关于区间DP,可以参照这个博客 http://blog.csdn.net/dacc123/article/details/50885903

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
int dp[30][30][30][30];
int a[30];
int b[30];
int n;
int sum;
int dfs(int x1,int y1,int x2,int y2,int sum)
{
    if(dp[x1][y1][x2][y2])
        return dp[x1][y1][x2][y2];
    if((x1>y1)&&(x2>y2))
        return 0;
    int num=0;
    if(x1<=y1)
    {
        num=max(num,sum-dfs(x1+1,y1,x2,y2,sum-a[x1]));
        num=max(num,sum-dfs(x1,y1-1,x2,y2,sum-a[y1]));
    }
    if(x2<=y2)
    {
        num=max(num,sum-dfs(x1,y1,x2+1,y2,sum-b[x2]));
        num=max(num,sum-dfs(x1,y1,x2,y2-1,sum-b[y2]));
    }
    dp[x1][y1][x2][y2]=num;
    return dp[x1][y1][x2][y2];
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            sum+=b[i];
        }
        memset(dp,0,sizeof(dp));
        printf("%d\n",dfs(1,n,1,n,sum));
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1137    Accepted Submission(s): 477 Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pi
ShenduCC
2018/04/27
6430
HDU 3309 Roll The Cube(bfs)
Roll The Cube Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 680    Accepted Submission(s): 272 Problem Description This is a simple game.The goal of the game is to roll two balls to two holes e
ShenduCC
2018/04/27
5700
Hdu 1789 Doing Homework again
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6538    Accepted Submission(s): 3900
熙世
2019/07/15
4470
HDU 1866 A + B forever!
A + B forever! Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1171    Accepted Submission(s): 268 Problem Description As always, A + B is the necessary problem of this warming-up contest. But t
ShenduCC
2018/04/26
5810
POJ 1651 Multiplication Puzzle(区间DP)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8107 Accepted: 5034 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes
ShenduCC
2018/04/26
6440
HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 27185    Accepted Submission(s): 6630 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些
Angel_Kitty
2018/04/09
9070
HDU 1074 Doing Homework(状压DP)
Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7660    Accepted Submission(s): 3454 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot
ShenduCC
2018/04/27
5440
HDU 4616 Game 树形DP
Problem Description   Nowadays, there are more and more challenge game on TV such as ‘Girls, Rush Ahead’. Now, you participate int a game like this. There are N rooms. The connection of rooms is like a tree. In other words, you can go to any other room by one and only one way. There is a gift prepared for you in Every room, and if you go the room, you can get this gift. However, there is also a trap in some rooms. After you get the gift, you may be trapped. After you go out a room, you can not go back to it any more. You can choose to start at any room ,and when you have no room to go or have been trapped for C times, game overs. Now you would like to know what is the maximum total value of gifts you can get.
风骨散人Chiam
2020/10/28
3060
HDU-1054 Strategic Game(树形DP)
Strategic Game Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 17 Accepted Submission(s) : 11 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Bob enjoys playi
ShenduCC
2018/04/25
5990
POJ-1191-棋盘分割(动态规划)
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13593 Accepted: 4846 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘。(每次切割都只能沿着棋盘格子的边进行) 原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在
ShenduCC
2018/04/25
9230
博弈专题入门总结(Nim 巴什 SG等证明+例题)
一堆n个物品,两个人轮流从这堆物品中取物, 规定每次取[1,m]个,最后取光者得胜,问先手必胜还是后手必胜。
Here_SDUT
2022/06/29
2.2K0
博弈专题入门总结(Nim 巴什 SG等证明+例题)
P3507 [POI2010]GRA-The Minima Game
题目描述 Alice and Bob learned the minima game, which they like very much, recently. The rules of the game are as follows. A certain number of cards lies on a table, each inscribed with a positive integer. The players make alternate moves, Alice making the fir
attack
2018/04/13
6940
HDU 4605 Magic Ball Game(可持续化线段树,树状数组,离散化)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2489    Accepted Submission(s): 759 Problem Description When the magic ball game turns up, Kimi immediately falls in it. The inter
ShenduCC
2018/04/27
6700
HDUOJ-------Naive and Silly Muggles
Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 61    Accepted Submission(s): 39 Problem Description Three wizards are doing a experiment. To avoid from bothering, a special
Gxjun
2018/03/21
6410
HDU 1086 计算几何 判断线段相交
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6456    Accepted Submission(s): 3116
csxiaoyao
2019/02/18
5820
HDUOJ--1058HangOver
HangOver Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S
Gxjun
2018/03/21
5530
HDUOJ--1058HangOver
HDU-1520 Anniversary party(树形DP)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7566 Accepted Submission(s): 3321 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the U
ShenduCC
2018/04/25
5000
最大子矩阵(C/C++)
解决该问题的常用方法是使用动态规划。先计算出每一行的前缀和,然后对于每一列的起始和终止位置,计算出该区域内每一行的和,得到一个一维数组。再对该一维数组使用动态规划求解最大子数组和的问题,得到最大子矩阵的元素之和。
摆烂小白敲代码
2024/09/23
1790
最大子矩阵(C/C++)
HDUOJ---2642Stars(二维树状数组)
Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 975    Accepted Submission(s): 420 Problem Description Yifenfei is a romantic guy and he likes to count the stars in the sky. To make the prob
Gxjun
2018/03/22
4830
HDUOJ---2642Stars(二维树状数组)
HDOJ1175连连看 DFS
连连看 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25178 Accepted Submission(s): 6230
谙忆
2021/01/19
5520
相关推荐
HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验