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. 【C++】线程_beginthreadex参数说明

    unsigned long _beginthreadex( void * _Security, //第1个参数:安全属性,NULL为默认安全属性 unsigned _StackSize, //第2个参 ...

  2. (转)SCR, PCR, ESCR, PTS, DTS

    1.       SCR SCR是存在于PS中的,即PS的pack里面的一个field.他用来指定这个PS的pack期望的到达decoder的时间. 2.       ESCR ESCR是位于PES里 ...

  3. (转)YUV420存储格式

    YUV格式有两大类:planar和packed.对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V.对于packed的YUV格式,每个像素点的Y,U ...

  4. 利用kseq.h parse fasta/fastq 文件

    在分析中经常需要统计fasta/fastq文件的序列数和碱基数, 但是没有找到一些专门做这件事的小工具,可能是这个功能太简单了: 之前用自己写的perl的脚本统计这些信息, 当fastq文件非常大时, ...

  5. win7 键盘

    请在任务栏的空白处右击,在弹出的选项中选择“工具栏”,再在“Table PC输入面板”选项中打勾,这里任务栏的最右边就会出现一个Table PC 输入面板”的图标

  6. 实操演练!MathType几个绝妙小技巧!

    在论文中编写公式时MathType绝对是很多人不二的选择,它的功能比较完善,操作比较方便,包含的符号模板很多,易学易上手,这些都是它的优点.但是在使用MathType时,还有很多绝妙的小技巧,使用起来 ...

  7. HEVC compressGOP 接口

    HEVC编码调用compressGOP()来实现一个GOPSize 图像序列的编码.在reference code里,真正做compressGOP编码之前,需要存GOPSize帧YUV在m_cList ...

  8. 加L“”

    error C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型 初学.net,编写如下代码运行,竟然提示错误(error C2665: “AfxMessageBo ...

  9. JavaScript 取对象的值时用点和中括号的区别

    用点的时候 点后面是对象的真实属性名称, 用中括号的时候 中括号里面是变量或者字符串

  10. Extjs学习笔记--(二)

    1.配置实用Extjs <link href="Extjs/resources/css/ext-all.css" rel="stylesheet" /&g ...