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. 简单的web三层架构系统【第五版】

    接上一版,今天差不多就是三层架构后台代码的完结了,这一版写完,接下来就是前台的制作了,前台不太熟悉,还在深入学习.过一段时间在写,今天先把后台代码写完. 三层架构包括DAL层, BLL层, UI层(也 ...

  2. POJ 3623 Best Cow Line, Gold(字符串处理)

    题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再 ...

  3. (转)ios跳转到通用页面

    在代码中调用如下代码: [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"prefs:root=LOCATION ...

  4. XGPush集成(信鸽集成)demo

    #import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #defi ...

  5. JavaSE_ API常用对象 总目录(11~14)

    JavaSE学习总结第11天_开发工具 & API常用对象111.01 常见开发工具介绍11.02 Eclipse和MyEclipse的概述11.03 Eclipse的下载安装及卸载11.04 ...

  6. UVa 12299 RMQ with Shifts(线段树)

    线段树,没了.. ----------------------------------------------------------------------------------------- # ...

  7. WPF多线程下载文件,有进度条

    //打开对话框选择文件         private void OpenDialogBox_Click(object sender, RoutedEventArgs e)         {     ...

  8. java list基本用法

    List<E>([]内的内容可省略),与数组类似: 实例化:List[<数据类型>] list = new ArrayList[<数据类型>](); 获得集合内元素 ...

  9. Nmon的安装及使用

    1.安装软件 1) 用root用户登录系统,建立目录:#mkdir  /nmon 2) 通过FTP将下载的nmon工具上传至服务器 192.168.40.212目录/nmon下. 3) 修改tar包权 ...

  10. mybatis字段名与类属性名不相同的冲突

    新建一个表 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no ), order_price FLOAT ); ...