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还是不敏感啊,虽然 ...
随机推荐
- css案例学习之float浮动
代码: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- 快速配置 Samba 将 Linux 目录映射为 Windows 驱动器
原文链接 samba client ubuntu redhat ubuntu gui tools 1,列出某个IP地址所提供的共享文件夹 smbclient -L 198.168.0.1 2,在s ...
- Embedded software develop step
x86 –>embeded so you you must familiar with x86 first-
- JIRA官方:JIRA报表与分析
访问重要的问题 JIRA系统内置的过滤器可以使你快速访问最重要的问题.通过保存和收藏自定义的过滤器,你可以随时了解项目和团队的优先级. 保持团队同步 创建一个过滤器,可以保存你的任何搜索条件.通过分享 ...
- pyspark 写 logistic regression
import random as rd import math class LogisticRegressionPySpark: def __init__(self,MaxItr=100,eps=0. ...
- 关于 .crash 分析
这里只给出其中 一种方式. 1. 建议 桌面 建 个文件夹 appxx ,然后 将那个闪退 对应的 包 xxx.app 放入 appxx文件夹 2. 打开终端cd命令,进入该文件夹 3.在命令 ...
- C#中Abstract和Virtual 【转】
http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html 在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在 ...
- Codeforces 551C GukiZ hates Boxes 二分答案
题目链接 题意: 一共同拥有n个空地(是一个数轴,从x=1 到 x=n),每一个空地上有a[i]块石头 有m个学生 目标是删除全部石头 一開始全部学生都站在 x=0的地方 每秒钟每一个学生都 ...
- CSDN博文大赛火爆开启
俗话说的好,程序猿会写博,谁也挡不住! 是不是每一个开发人员都能写出好博文,这个非常难说,但能够肯定的是,能写出好博文的,一定是优秀的程序猿! 写作即思考,养成写博文的习惯,既能帮自己整理技术思路,也 ...
- 只获取linux ip的命令
ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'