Play Game

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?

InputThe 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 a i (1≤a i≤10000). The third line contains N integer b i (1≤bi≤10000).OutputFor 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[al][ar][bl][br]记录的是在a的区间只剩下al~ar,b的区间只剩下bl~br的时候,Alice能得到的最大值
那么只需要考虑四种不同的取法并从中取得最优的方案,sum-(Alice上一个状态中Bob拿的值)中取最大
 
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; int a[],b[];
int dp[][][][]; int dfs(int al,int ar,int bl,int br,int sum){
if(al>ar&&bl>br){
return ;
}
if(dp[al][ar][bl][br]>-) return dp[al][ar][bl][br];
int ma=;
if(al<=ar){
ma=max(ma,sum-dfs(al+,ar,bl,br,sum-a[al]));
ma=max(ma,sum-dfs(al,ar-,bl,br,sum-a[ar]));
}
if(bl<=br){
ma=max(ma,sum-dfs(al,ar,bl+,br,sum-b[bl]));
ma=max(ma,sum-dfs(al,ar,bl,br-,sum-b[br]));
}
dp[al][ar][bl][br]=ma;
return ma;
}
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int sum=;
for(i=;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
for(i=;i<=n;i++){
scanf("%d",&b[i]);
sum+=b[i];
}
memset(dp,-,sizeof(dp));
printf("%d\n",dfs(,n,,n,sum));
}
return ;
}

HDU - 4597 Play Game(博弈dp)的更多相关文章

  1. hdu 4597 Play Game 区间dp

    Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...

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

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

  3. HDU 4597 Play Game (DP,记忆化搜索)

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

  4. 博弈dp入门 POJ - 1678 HDU - 4597

    本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...

  5. hdu 4778 Gems Fight! 博弈+状态dp+搜索

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...

  6. HDU 5623 KK's Number (博弈DP)

    KK's Number 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/K Description Our lovely KK h ...

  7. hdu 4597 Play Game(记忆化搜索)

    题目链接:hdu 4597 Play Game 题目大意:给出两堆牌,仅仅能从最上和最下取,然后两个人轮流取,都依照自己最优的策略.问说第一个人对多的分值. 解题思路:记忆化搜索,状态出来就很水,dp ...

  8. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  9. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  10. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

随机推荐

  1. 普通平衡树 - Treap

    怕被学弟怼 : "你的博客上没有Treap模板啊?" #include <cstdio> #include <cstring> #include <a ...

  2. 让人蛋疼的“Oracle.DataAccess.dll”

    项目介绍:为前台网站提供rest接口来操作erp相关数据 涉及db:oracle11 技术方案:因为erp是用remoting来调用,我想rest实现部分调用remoting来操作减少耦合,当然性能上 ...

  3. UGUI性能优化

    http://www.cnblogs.com/suoluo/p/5417152.html http://blog.csdn.net/uwa4d/article/details/54344423 htt ...

  4. C# partial 说明(转)

    http://www.cnblogs.com/Echo_saq/archive/2012/11/19/2777058.html 1. 什么是局部类型? C# 2.0 引入了局部类型的概念.局部类型允许 ...

  5. RequireJS 也可以引入 VUE

    RequireJS 也可以引入 VUE 由于 FastAdmin 是使用 RequireJS 导入 JS 模块的. 有人想把 VUE 也引入进去,虽然说也是可以,VUE 还是推荐使用 Webpack ...

  6. python if语句,while语句

    一,if语句: python中最常用的判断语句,基本格式: 1.if else格式 if  条件: 结果 else: 结果 """ if 条件: 满足条件执行代码 els ...

  7. FS系统开发设计(思维导图)

      FS系统开发设计(思维导图) 最近做了一个小系统,公司应急,要对各个部门进行费用成本核算分摊,做运维,苦于无奈,简简单单的设计了一下,模仿用友ERP软件,首先对DB进行了初步设计,总体如下: 未完 ...

  8. Sqoop-从hive导出分区表到MySQL

    经多次验证,发现并没有特殊的方法能够直接把多个分区一次性读入,并插入MySQL的方法,以后发现会在此添加. Sqoop只提供了从MySQL导入到HIVE分区表的相关参数,反向并无特别参数. 从HIVE ...

  9. grunt 压缩js css html 合并等配置与操作详解

    module.exports = function(grunt){ //1.引入 grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.loadNpmTa ...

  10. BZOJ3170:[TJOI2013]松鼠聚会

    题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3170 通过分析可以发现,题目所说的两点之间的距离就是切比雪夫距离. 两点之间欧几里得距离:\( ...