Play Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 54 Accepted Submission(s): 36

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 a
i (1≤a
i≤10000). The third line contains N integer b
i (1≤b
i≤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
 
Source
 
Recommend
liuyiding
这题就是一个区间dp,因为,每一次取都,可以取两个队列的头和尾,所以,就是一个二维的区间dp,我们用dp[al][ar][bl][br]表示,从第一个数列从al 到ar,第二个数列从,bl到br,当前这个人具有第一选择权的最大分值,那么我们,可以取两个队列的头和尾,就有4个状态转移方程了!用一个记忆化搜索就可以了!
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 26
int suma[MAXN],sumb[MAXN],pa[MAXN],pb[MAXN],dp[MAXN][MAXN][MAXN][MAXN];
int dfs(int al,int ar,int bl ,int br)
{
if(dp[al][ar][bl][br]!=-1)
return dp[al][ar][bl][br];
dp[al][ar][bl][br]=0;
if(al<=ar)
dp[al][ar][bl][br]=suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al+1,ar,bl,br);
if(al<=ar)
dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar-1,bl,br));
if(bl<=br)
dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl+1,br));
if(bl<=br)
dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl,br-1));
return dp[al][ar][bl][br];
}
int main ()
{
int n,i,tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d",&n);
suma[0]=sumb[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&pa[i]);
suma[i]=suma[i-1]+pa[i];
}
for(i=1;i<=n;i++)
{
scanf("%d",&pb[i]);
sumb[i]=sumb[i-1]+pb[i];
}
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(1,n,1,n));
}
return 0;
}

hdu4597 Play Game的更多相关文章

  1. hdu4597 区间dp

    //Accepted 1784 KB 78 ms //区间dp //dp[l1][r1][l2][r2] 表示a数列从l1到r1,b数列从l2到r2能得到的最大分值 // #include <c ...

  2. hdu4597 Play Game(DFS)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4597 题意 Alic ...

  3. hdu4597 Play Game DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 感觉很不错的区间DP,又做了一遍,感觉自己对边界的处理还是很欠缺 代码: #include< ...

  4. hdu4597 Play Game 区间DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 全国邀请赛通化赛区第8题--题目重现 思路: 区间DP的思想,想法是队友想出来的,感觉很秒,自己 ...

  5. Alice and Bob(不断补充)

    我之前做过一些博弈的题目,以为博弈都是DP,结果被坑了很多次,其实博弈有很多种,在此,把我见过的类型都搬上来. 1,HDU3951(找规律) 题意:把n枚硬币围成一个圆,让Alice和Bob两个人分别 ...

随机推荐

  1. Windows 配置JAVA的环境变量

    Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...

  2. BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队( RMQ )

    RMQ.. ------------------------------------------------------------------------------- #include<cs ...

  3. jquery+easy ui 实现表格列头筛选

    示例代码 1.筛选的下拉 <a href="javascript:void(0)" id="filterStatus" class="easyu ...

  4. python自学笔记(三)python基本数据类型之列表list

    列表list特性概括 1.有序集合 2.通过偏移来索引,从而读取数据 3.支持嵌套 4.可变的类型(dict 字典也是可变的) (1)切片 a = [1,2,3,4,5,6,7] 正向索引 a[0:4 ...

  5. windows文件快速搜索软件推荐

    everything文件搜索工具,可以快速搜索windows下的文件

  6. spring与redis集成之aop整合方案

    java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了. 将spring与redis缓存集成,其实也是使用jedis框架,只不过 ...

  7. 再探java基础——break和continue的用法

    再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...

  8. ThinkPHP 3.1.2 查询方式的一般使用2

    //select id1> and id2< 默认是and $data['id']=array(array('gt',$id1),array('lt',$id2)); // $data[' ...

  9. Linux Centos 系统上安装BT客户端 Transmission

    Linux Centos 系统上安装BT客户端 Transmission   Transmission是一种BitTorrent客户端,特点是一个跨平台的后端和其上的简洁的用户界面,以MIT许可证和G ...

  10. commview for wifi 破解无线

    相信了解无线网络的读者都知道安全性是无线网络的先天不足,正是因为他的传播通过空气,所以信号很容易出现外泄问题,相比有线网络来说信号监听变得非常简单. 部分用户通过WEP加密的方式来保护网络通讯数据包避 ...