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下获取和设置IP

    在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作. 那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体. /* Interfa ...

  2. bootstrap-fileinput上传文件的插件使用总结----编辑已成功上传过的图片

    http://plugins.krajee.com/file-plugin-methods-demo 具体操作 http://plugins.krajee.com/file-preview-manag ...

  3. 忘记SVN密码怎么办

    1:下载TSvnPwd.exe 2:使用wireshark抓包.例如: PROPFIND /svn/dev2/!svn/vcc/default HTTP/1.1Host: 192.168.156.1: ...

  4. ROS数据可视化工具Rviz和三维物理引擎机器人仿真工具V-rep Morse Gazebo Webots USARSimRos等概述

    ROS数据可视化工具Rviz和三维物理引擎机器人仿真工具V-rep Morse Gazebo Webots USARSimRos等概述 Rviz Rviz是ROS数据可视化工具,可以将类似字符串文本等 ...

  5. GUC-7 同步锁 Lock

    import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /* * 一.用于解决 ...

  6. day10--异步IO\数据库\队列\缓存

    上节回顾: 线程  vs  进程 https://www.cnblogs.com/alex3714/articles/5230609.html https://www.cnblogs.com/alex ...

  7. linux 101 hacks 4stat diff ac

    stat 命令 stat 命令那个可以用来查看文件或者文件系统的状态和属性.显示一个文件或目录的属性 $ stat /etc/my.cnf File: `/etc/my.cnf' Size: Bloc ...

  8. Excel快速数据处理

    年底各位领导都要统计各种报表数据,Excel技能捉襟见肘啊! 同一xlsx文件下同一Sheet下的数据引用 同一xlsx文件下不同Sheet下的数据引用 同一文件夹下的不同xlsx文件下的数据引用 不 ...

  9. 问题:SpringBoot访问不到Controller

    SpringBoot正常启动,其它配置都正常,以下是控制台打印: 解决方法: 将controller与application配置文件同层,是访问时无法扫描到相应的controller,故无法映射到相应 ...

  10. 第一个iOS程序:Hello iOS

    今天我们来创建第一个iOS程序:Hello iOS!不需要写任何代码就能实现: