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. 1625 codevs数字金字塔

    1625 数字金字塔 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 考虑在下面被显示的数字金字塔. 写 ...

  2. loopqueue

    import java.util.Arrays; public class loopQueue <E>{ public Object[] data=null; private int ma ...

  3. Angular 隨記

    Windows下更新Node 和NPM方法 管理員模式打開powershell 執行以下命令: Set-ExecutionPolicy Unrestricted -Scope CurrentUser ...

  4. NSString *const 和 const NSString * 的区别

    1.变量存储的指针可变,变量存储的值不可变 //A modifiable pointer to a constant NSString (its value can't be modified) &q ...

  5. poj 3463 Sightseeing——次短路计数

    题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...

  6. JavaScript创建对象的几种重要模式

    一.工厂模式 1. 代码示例 function person(name, age) { var p = new object(); p.name = name; p.age = age; p.sayN ...

  7. ORA-12514: TNS: no listener 解决方案

    服务端:oracle 11g 客户端: pl/sql 问题描述: 用客户端 pl/sql 连接登录的时候,提示 "ORA-12514: TNS: no listener". 在服务 ...

  8. JSF使用HTML5的custom attribute

    只需要在页面引用这样的命名空间: xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"之后,在JSF的控件的html5属性加上前缀&quo ...

  9. (转)C#用Linq实现DataTable的Group by数据统计

    本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序 ...

  10. webrtc doubango linphone

    1.doubango官网:http://www.doubango.org/ 2.doubango是一个开源的VOIP基础平台, 并能用于嵌入式和桌面系统的开源框架,该框架使用ANSCI-C编写,具有很 ...