hdu 4597 Play Game(区间dp,记忆化搜索)
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?
The first line contains an integer T (T≤), indicating the number of cases.
Each case contains lines. The first line is the N (N≤). The second line contains N integer ai (≤ai≤). The third line contains N integer bi (≤bi≤).
For each case, output an integer, indicating the most score Alice can get.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int sum1[],sum2[];
int a[];
int b[];
int dp[][][][];
int dfs(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]=0;
int sum=;
int ans=;
if(l1<=r1)
{
sum+=sum1[r1]-sum1[l1-];
}
if(l2<=r2)
{
sum+=sum2[r2]-sum2[l2-];
}
if(l1<=r1)
{
ans=max(ans,sum-min(dfs(l1+,r1,l2,r2),dfs(l1,r1-,l2,r2)));
}
if(l2<=r2)
{
ans=max(ans,sum-min(dfs(l1,r1,l2+,r2),dfs(l1,r1,l2,r2-)));
}
return dp[l1][r1][l2][r2]=ans;
}
int main()
{
int t;
int n;
cin>>t;
while(t--)
{
cin>>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",dfs(,n,,n));
}
return ;
}
hdu 4597 Play Game(区间dp,记忆化搜索)的更多相关文章
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- uva 10891 区间dp+记忆化搜索
https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...
- HDU - 5001 Walk(概率dp+记忆化搜索)
Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started t ...
- HDU - 6143 Killer Names(dp记忆化搜索+组合数)
题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的 ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- loj 1031(区间dp+记忆化搜索)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...
- BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】
题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...
随机推荐
- UML_行为图
活动图是UML用于对系统的动态行为建模的另一种常用工具,它描述活动的顺序,展现从一个活动到另一个活动的控制流.活动图在本质上是一种流程图.活动图着重表现从一个活动到另一个活动的控制流,是内部处理驱动的 ...
- RPATH与RUNPATH
RPATH与RUNPATH 时间 2011-11-01 21:46:44 Qt Labs China 原文 http://labs.qt.nokia.com.cn/2011/11/01/rpath- ...
- 云脉表格识别开放SDK接入
通过深度的引擎识别和文本处理技术给予表单提供了无与伦比的文档分析和数据的提取功能,云脉表单识别包含了先进的模板学习和文本.图像分析提取技术,通过模板元素定义表单,将整个南表单生命周期和生产数据以自动化 ...
- C# 基础知识 (一).概念与思想篇
在C#中有一些我自己认为比较独特的知识点,这些知识点是我经常使用的知识,但对它们的了解还是比较少的,所以通过查找资料学习,总结了这些独特的知识点并简单叙述,第一篇主要是一些概念和思想方面的知识.(后面 ...
- Newton‘ method 的优缺点
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzE1Mjg5NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- C++ 顶层 const
我的主力博客:半亩方塘 本文的主要參考来源来自于:C++ Primer 中文版(第 5 版) 第 57 面至第 58 面 1. 顶层 const 与底层 const 概念 我们知道,指针本身是一个对象 ...
- mysql 新增 删除用户和权限分配
请一定安此步骤来创建新的用户. 1. 新增用户 mysql>insert into mysql.user(Host,User,Password) values("localhost&q ...
- 【部分枚举】【3-21个人赛】ProblemH
Problem H Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- IDE idea 更换项目的JDK步骤
1.如图:
- asp.netGridView使用技巧
GridView属性介绍 AutoGenerateColumns 如果为true表示自动生成数据列,如果为false关闭自动生成状态 何为自动生成数据列 比如这么一个表格: country name ...