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. C#第三方zip解压压缩工具,带事例源码

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using ICSharpCode. ...

  2. activity的横屏和竖屏设置

    主要在清单文件这样配置: <application android:allowBackup="true" android:icon="@drawable/ic_la ...

  3. HTML静态网页(图片热点、网页划区、拼接及表单的使用)

    图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例:   网页的拼接: 在一个 ...

  4. 简单天气应用开发——自定义TableView

    顺利解析JSON数据后,天气数据已经可以随意提取了,现在要做的就是建立一个简单的UI. 实况信息较为简单,几个Lable就可以解决.主要是七天天气预报有点麻烦,那是一个由七个字典构成的数组,需要提取出 ...

  5. 数组名和指针的深入理解(C++)

    指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很多程序设计者就被搞糊涂了.魔幻数组名请看程序(本文程序在WIN32平台下编译): #includ ...

  6. MySql 日期转字符串

    1.date_format 日期转字符串 select date_format(now(),'%Y-%m-%d %H:%i:%s'); 2.str_to_date 字符串转日期 select str_ ...

  7. codeforces 417D. Cunning Gena 状压dp

    题目链接 D. Cunning Gena time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. cocos2d-x Android环境搭建

    1.Java虚拟机.分32位和64位.64位: jdk-8u11-windows-x64-8.0.11.12.1406275777 环境变量配置,我的电脑右击->属性->高级系统设置-&g ...

  9. 24_Core Data Demo

    今天开始学习Core Data,类似于数据库,可以永久保存数据.不过当把App从iPhone删掉之后就没有了.可以用来保存App的运行数据. 参考链接:iOS Swift教程 Core Data 概述 ...

  10. Spring Boot使用Druid和监控配置

    Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource 整体步骤: (1)    --   Druid简单介绍,具体看官网: (2)     ...