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. linux终端操作快捷键

    终端操作快捷键: 新建家目录下终端窗口:Ctrl+Alt+t在当期当前路径下新建终端窗口:Ctrl+Shift+n退出终端窗口:Ctrl+Shift+q 多个终端窗口之间相互切换:Tab+Alt 终端 ...

  2. Flask:初见

    Windows 10家庭中文版,Python 3.6.4 从Flask官网开始学起. 介绍 Flask是一个Python的Web开发微框架,基于Werkzeug.Jinja2模块(and good i ...

  3. javascript本地缓存方案-- 存储对象和设置过期时间

    cz-storage 解决问题 1. 前端js使用localStorage的时候只能存字符串,不能存储对象 cz-storage 可以存储 object undefined number string ...

  4. 20155225 实验二《Java面向对象程序设计》实验报告

    20155225 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...

  5. linux ncat命令

    netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...

  6. day4 计算器

    作业:计算器开发 (1)实现加减乘除及拓号优先级解析: (2)用户输入 1 - 2 * ( (60-30 +(-40/5) * (-9-2*5/-3 + 7 /3*99/4*2998 +10 * 56 ...

  7. 8-13 Just Finish it up uva11093

    题意:环形跑道上有n n<=100000 个加油站  编号为1-n  第i个加油站可以加油pi加仑   从加油站i开到下一站需要qi加仑   你可以选择一个加油站作为起点 初始油箱为空   如果 ...

  8. Linux下安装Zookeeper

    Zookeeper是一个协调服务,可以用它来作为配置维护.名字服务.分布式部署: 下面,我来分享一下在Linux下安装Zookeeper的整个步骤,让大家少走弯路. 一.Zookeeper下载 [ro ...

  9. HTML Input 表单校验之datatype

    凡要验证格式的元素均需绑定datatype属性,datatype可选值内置有10类,用来指定不同的验证格式. 如果还不能满足您的验证需求,可以传入自定义datatype,自定义datatype是一个非 ...

  10. 深入理解SQL的四种连接,左外连接,右外连接,内连接,全连接

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...