Play Game

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

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 ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤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
 

只有每种情况记忆化搜索下就可以了

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/24 12:37:04
File Name :F:\2013ACM练习\比赛练习\2013通化邀请赛\1008.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int a[],b[];
int sum1[];
int sum2[];
int dp[][][][];
int solve(int l1,int r1,int l2,int r2)
{
if(dp[l1][r1][l2][r2] != -)return dp[l1][r1][l2][r2];
if(l1 > r1 && l2 > r2)
return dp[l1][r1][l2][r2] = ;
int ans = ;
int sum = ;
if(l1 <= r1)
sum += sum1[r1] - sum1[l1-];
if(l2 <= r2)
sum += sum2[r2] - sum2[l2-];
if(l1 <= r1)
{
ans = max(ans,sum - solve(l1+,r1,l2,r2));
ans = max(ans,sum - solve(l1,r1-,l2,r2));
}
if(l2 <= r2)
{
ans = max(ans,sum - solve(l1,r1,l2+,r2));
ans = max(ans,sum - solve(l1,r1,l2,r2-));
}
return dp[l1][r1][l2][r2] = ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum1[] = sum2[] = ;
for(int i = ;i <= n;i++)
{
scanf("%d",&a[i]);
sum1[i] = sum1[i-] + a[i];
}
for(int i = ;i <= n;i++)
{
scanf("%d",&b[i]);
sum2[i] = sum2[i-] + b[i];
}
memset(dp,-,sizeof(dp));
printf("%d\n",solve(,n,,n));
}
return ;
}

HDU 4597 Play Game (DP,记忆化搜索)的更多相关文章

  1. 【bzoj5123】[Lydsy12月赛]线段树的匹配 树形dp+记忆化搜索

    题目描述 求一棵 $[1,n]$ 的线段树的最大匹配数目与方案数. $n\le 10^{18}$ 题解 树形dp+记忆化搜索 设 $f[l][r]$ 表示根节点为 $[l,r]$ 的线段树,匹配选择根 ...

  2. 【BZOJ】1415 [Noi2005]聪聪和可可 期望DP+记忆化搜索

    [题意]给定无向图,聪聪和可可各自位于一点,可可每单位时间随机向周围走一步或停留,聪聪每单位时间追两步(先走),问追到可可的期望时间.n<=1000. [算法]期望DP+记忆化搜索 [题解]首先 ...

  3. !HDU 1078 FatMouse and Cheese-dp-(记忆化搜索)

    题意:有一个n*n的格子.每一个格子里有不同数量的食物,老鼠从(0,0)開始走.每次下一步仅仅能走到比当前格子食物多的格子.有水平和垂直四个方向,每一步最多走k格,求老鼠能吃到的最多的食物. 分析: ...

  4. [题解](树形dp/记忆化搜索)luogu_P1040_加分二叉树

    树形dp/记忆化搜索 首先可以看出树形dp,因为第一个问题并不需要知道子树的样子, 然而第二个输出前序遍历,必须知道每个子树的根节点,需要在树形dp过程中记录,递归输出 那么如何求最大加分树——根据中 ...

  5. poj1664 dp记忆化搜索

    http://poj.org/problem?id=1664 Description 把M个相同的苹果放在N个相同的盘子里,同意有的盘子空着不放,问共同拥有多少种不同的分法?(用K表示)5.1.1和1 ...

  6. 状压DP+记忆化搜索 UVA 1252 Twenty Questions

    题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...

  7. ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. Poor Ramzi -dp+记忆化搜索

    ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)- K. ...

  8. POJ 1088 DP=记忆化搜索

    话说DP=记忆化搜索这句话真不是虚的. 面对这道题目,题意很简单,但是DP的时候,方向分为四个,这个时候用递推就好难写了,你很难得到当前状态的前一个真实状态,这个时候记忆化搜索就派上用场啦! 通过对四 ...

  9. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  10. HDU 2476 String painter(记忆化搜索, DP)

    题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...

随机推荐

  1. Little C Loves 3 I

    CF#511 div2 A 现场掉分赛(翻车),就是这道题被叉了...qwq 其实就是一道水题: 因为CF有spj,所以直接构建特殊情况就行了. 当 n 是3的倍数的时候,显然 1,1,(n-2) 显 ...

  2. 通过field:global给子元素添加css样式

    {dede:arclist row=5 typeid=200} <li [field:global runphp=’yes’ name=autoindex](@me==1)?@me=”class ...

  3. 如何阻止点击scrollviewer里面的单位内容时,自动滚动

    <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="FocusVisualStyle ...

  4. 关于json中转义字符/正斜杠的问题。

    1.首先有关转义字符 可以看百度百科: 先不管/是否需要转义,我们去json的官方网站去看看:http://www.json.org/ 可见有这个,那么意思是 json中 又规定建议了一下,意思是虽然 ...

  5. Centos之链接命令

    链接命令:ln  (link) ln -s [源文件] [目标文件] 功能描述:生成链接文件 选项: -s 创建软链接 硬链接特征: 1,拥有相同的i节点和存储block块,可以看作是同一个文件: 2 ...

  6. SQL2008数据库导出到SQL2000全部步骤过程

    2008转到2000的步骤 1. 在sql2008上生成for 2000版本的数据库脚本 -- 打开"对象资源管理器"(没有的话按F8), 连接到你的实例, 右键要转到2000的库 ...

  7. Ubuntu CEPH快速安装

    一.CEPH简介 不管你是想为云平台提供Ceph 对象存储和/或 Ceph 块设备,还是想部署一个 Ceph 文件系统或者把 Ceph 作为他用,所有 Ceph 存储集群的部署都始于部署一个个 Cep ...

  8. c语言双向循环链表

    双向循环链表,先来说说双向链表,双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继 ...

  9. Python学习:socket.gaierror: [Errno -8]

    在终端内打开python模式,利用如下代码查询本机hostname,这里举例为“xxMacBookPro.local”: import socket socket.gethostname() 在/et ...

  10. 毕设demo写好

    2015年1月20日 14:41:47 阶段性暂停!! 把运行结果截图给了老师,老师说先整理下文档,然后下学期来了再部署到服务器上. 那么,下学期来了,估计也要把Epm和CR1000什么的搞好了. 先 ...