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. LeetCode 315. Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  2. bzoj 3709: [PA2014]Bohater 贪心

    题目: 在一款电脑游戏中,你需要打败\(n\)只怪物(从\(1\)到\(n\)编号).为了打败第\(i\)只怪物,你需要消耗\(d_i\)点生命值,但怪物死后会掉落血药,使你恢复\(a_i\)点生命值 ...

  3. 在winform下实现左右布局多窗口界面的方法(二)

    这篇文章主要介绍了在winform下实现左右布局多窗口界面的方法之续篇 的相关资料,需要的朋友可以参考下 在上篇文章在winform下实现左右布局多窗口界面的方法(一)已经实现了左右布局多窗口界面,今 ...

  4. 基于dubbo的SOA项目改造

    工程改造: 1.原来工程删除掉表现层的模块,将表现层独立出来. 2.将原来的工程改造.将service的打包方式改为改为war. 3.在service模块中添加web.xml文件 4.在web.xml ...

  5. ArcGis教程

    91卫图助手帮助中心-如何导出ArcGIS Server瓦片格式并进行发布 http://help.91weitu.com/rhdcarcgis%20serverwpgsbjxfb.html ArcG ...

  6. LTE230方案示意图

    普天LTE230产品及解决方案   责任编辑:耿鹏飞 2016.12.26 16:42 来源:通信世界网 LTE230 普天   一.LTE230系统特点 (一)系统背景 随着社会的快速发展,能源危机 ...

  7. pushd命令

    1)功能pushd命令常用于将目录加入到栈中,加入记录到目录栈顶部,并切换到该目录:若pushd命令不加任何参数,则会将位于记录栈最上面的2个目录对换位置 2)语法(1)格式:pushd  [目录 | ...

  8. 使用script转储终端命令输出,或者录制并播放session的内容

    摘自:http://leohot.blog.163.com/blog/static/1348656022012729113658473/ 注意: 1. 启动script时没有指定文件名,它会自动记录到 ...

  9. spring bean管理

    轻量级,无侵入 Bean管理 1 创建applicationContext.xml 2 配置被管理的Bean 3 获取Bean pom.xml配置 <dependency> <gro ...

  10. windows中git输错密码后不能重新输入的问题

    当输入密码错误后,git会把密码保存在windows的你的当前用户名的缓存中. 我们可以在控制面板中寻找到这个缓存,再删掉就可以了. 首先,打开“控制面板”,把“查看方式”改为“大图标”,再打开“用户 ...