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

#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;
}

HDU 4597 Play Game(DFS,区间DP)的更多相关文章

  1. hdu 4597 + uva 10891(一类区间dp)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...

  2. HDU 4597 Play Game(区间DP(记忆化搜索))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...

  3. HDU 5151 Sit sit sit 区间DP + 排列组合

    Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...

  4. HDU 2476 String painter (区间DP)

    题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...

  5. hdu 5115 Dire Wolf(区间dp)

    Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful w ...

  6. HDU 2476 String painter(区间DP)

    String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...

  8. HDU 5115 Dire Wolf ——(区间DP)

    比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: #inc ...

  9. HDU 5151 Sit sit sit 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5151 题解: 有n个椅子,编号为1到n. 现在有n个同学,编号为1到n,从第一个同学开始选择要坐的位 ...

  10. HDU - 4597 Play Game(博弈dp)

    Play Game Alice and Bob are playing a game. There are two piles of cards. There are N cards in each ...

随机推荐

  1. yuv420格式分析

    http://blog.csdn.net/liuhongxiangm/article/details/9135791 http://blog.csdn.net/bluesky_sunshine/art ...

  2. RelativeLayout用代码兑现布局

    RelativeLayout用代码实现布局TextView txt1 = new TextView(this);      RelativeLayout.LayoutParams params = n ...

  3. NHibernate初学六之关联多对多关系

    1:创建三张表T_Course.T_Student.T_Middle:其中一个学生可以对应多个课程,一个课程也可以对应多个学生,用T_Middle存放它们的关系内容: CREATE TABLE [db ...

  4. easyui------添加中文文件

    添加中文文件: 官网地址:http://www.jeasyui.net/download/去jquery-easyui官网下载的文件里面找到easyui-lang-zh_CN.js文件,添加入代码里面 ...

  5. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  6. row_number()over函数的使用(转)

    (转)http://hi.baidu.com/122439049/blog/item/0c9c48131b2734d5f7039e13.html row_number() OVER (PARTITIO ...

  7. 查看内存使用情况:free

    free命令用于显示内存的使用情况,常见用法如下: [root@localhost ~]$ free # 以KB为单位显示内存使用情况 [root@localhost ~]$ free -m # 以M ...

  8. 基于麒麟座开发板2.0的MQTT实现例程

    链接--->https://sanwen8.cn/p/649shZ1.html OneNET现已全面适配标准MQTT协议,相信这一功能的增加会**便于开发者进行设备的接入. OneNET提供了M ...

  9. SuperSlide——再次接触

    原来自己很早之前就接触过SuperSlider这个神奇的插件,原谅自己又得了“健忘症”,因此昨天就把自己“坑”了一把: 前言: 交易一期的项目即将上线,在解决测试同事们提出的bug的时候,无意间看到页 ...

  10. Spring学习笔记--通过构造方法创建Bean

    如果一个bean没有默认的构造函数,那么可以通过工厂方法来构造一个bean.Spring通过<bean>元素的factory-method属性来装配工厂创建的Bean.下面例子中的Stag ...