HDU 4597 Play Game(DFS,区间DP)
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)的更多相关文章
- hdu 4597 + uva 10891(一类区间dp)
题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...
- HDU 4597 Play Game(区间DP(记忆化搜索))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...
- HDU 5151 Sit sit sit 区间DP + 排列组合
Sit sit sit 问题描述 在一个XX大学中有NN张椅子排成一排,椅子上都没有人,每张椅子都有颜色,分别为蓝色或者红色. 接下来依次来了NN个学生,标号依次为1,2,3,...,N. 对于每个学 ...
- HDU 2476 String painter (区间DP)
题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...
- hdu 5115 Dire Wolf(区间dp)
Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful w ...
- HDU 2476 String painter(区间DP)
String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...
- HDU 5115 Dire Wolf ——(区间DP)
比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: #inc ...
- HDU 5151 Sit sit sit 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5151 题解: 有n个椅子,编号为1到n. 现在有n个同学,编号为1到n,从第一个同学开始选择要坐的位 ...
- 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 ...
随机推荐
- Maven中央仓库——你可能不知道的细节
地址 —— 目前来说,http://repo1.maven.org/maven2/是真正的Maven中央仓库的地址,该地址内置在Maven的源码中,其它地址包括著名的ibiblio.org,都是镜像. ...
- 【转】【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
- ImageNet Classification with Deep Convolutional Neural Networks 论文解读
这个论文应该算是把深度学习应用到图片识别(ILSVRC,ImageNet large-scale Visual Recognition Challenge)上的具有重大意义的一篇文章.因为在之前,人们 ...
- [转载] PHP开发必看 编程十大好习惯
适当抽象 但是在抽象的时候,要避免不合理的抽象,有时也可能造成过渡设计,现在只需要一种螺丝刀,但你却把更多类型的螺丝刀都做出来了(而且还是瑞士军刀的样子..): 一致性 团队开发中,可能每个人的编程风 ...
- 转载:【原译】Erlang构建和匹配二进制数据(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/19/2727204.html Constructing and matching binarie ...
- tomcat日志神器--kibana
最近公司搭了套kibana的日志系统,感受比原来查看日志方便多了.记得以前查看日志是通过ssh到服务器,查看系统日志用vi查看器查看或者下载到本地,用logview查看搜索,可读性很低.自从用了kib ...
- js事件总结
事件冒泡: 什么是事件冒泡,就是最深dom节点触发事件,然后逐级向最外层触发事件.打个比方一棵dom tree:li<ul<div每级都有事件绑定,然后我们触发li的事件,这时ul上的事件 ...
- Java任务调度开源框架quartz学习
一.quartz学习 Java框架介绍:Quartz从入门到进阶 http://edu.yesky.com/edupxpt/233/2209233.shtml 1.例子:http://javacraz ...
- Supervisor安装与配置(非守护进程管理工具)
http://blog.csdn.net/xyang81/article/details/51555473
- C# CRC16 查表法
private static ushort[] crctab = new ushort[256]{ 0x0000, 0x1021, 0x2042, 0x306 ...