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. js中的点击事件(click)的实现方式

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  2. Python网络编程中的select 和 poll I/O复用的简单使用

    首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select ...

  3. 树结构ztree的 ajax交互的简单使用

    今天做前端页面要用到树结构,用了第三方插件ztree,搞了好久不过终于弄出来了,, 一点小心得.(用的版本 V3 ) 首先看下载的文件结构: 一:将要用到的CSS 和 JS 拷贝到工程中,我这里在工程 ...

  4. 无法确定要使用哪一版本的 ASP.NET Web Pages。

    若要使用此站点,请在站点的 web.config 文件中指定一个版本.有关详细信息,请参阅 Microsoft 支持站点上的以下文章: http://go.microsoft.com/fwlink/? ...

  5. 使用批处理自动发布IIS站点,基于IIS7及以上

    经过研究,终于使用批处理解决了站点发布步骤多的问题. 完整批处理如下: @set "sitePath=%~dp0" @echo 新建程序池 @C:\Windows\System32 ...

  6. 遍历js的obj中所有属性得key

    var obj = $("#jstree_default").jstree("get_checked"); for (var a in obj) { alert ...

  7. Oracle RMAN 学习:恢复

    Oracle RMAN 学习:恢复 6 rman恢复 Rman中的恢复对应restore,recover Restore,数据修复,利用备份集的数据文件来替换已损坏的数据文件或将其恢复到另外一个位置, ...

  8. JAVA web 相关知识点

    1: web的三个核心标准: URL: http   VS  https HTTP:  通信协议,客户端/服务器端信息交互方式; 特点是无状态:                 HTML: 2: HT ...

  9. android 定位的几种方式介绍

    [地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...

  10. 12-21C#电脑蓝屏效果(可以恶搞整人哦)、输入输出流(StreamReader/streamWriter)

    一.winform电脑蓝屏效果 第一种方法:基本操作: 第一步:创建一个新的C#窗体“Form1”: 第二步:在设计窗口中,更改其属性: 1)text属性:将form1的text属性中的文字取消掉,然 ...