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还是不敏感啊,虽然 ...
随机推荐
- javascript中的动画的实现以及运动框架的编写(1)
大家知道js可以做出好多很漂亮的动画,看上去很神奇,其实原理很简单,今天就讨论一下js动画的实现以及如何编写可以反复重用的运动框架. 首先做一个简单的例子,我这里有一个长50px 宽20px的长条形d ...
- 竹林蹊径-深入浅出Windows内核开发作者的博客
http://blog.csdn.net/blog_index http://blog.csdn.net/blog_index/article/details/6012054 http://downl ...
- idea编译工程时出现Error:java: 无效的目标发行版: 1.8
见图,从上述可以看出工程用的jdk1.7,而idea编译时采用的是1.8版本(应该idea新版本内置的jre是1.8吧,默认编译采用1.8) 修改:如下图 http://blog.csdn.ne ...
- 实用的PHP正则表达式
正则表达式是程序开发中一个重要的元素,它提供用来描述或匹配文本的字符串,如特定的字符.词或算式等.但在某些情况下,用正则表达式去验证一个字符串比较复杂和费时.本文为你介绍10种常见的实用PHP正则表达 ...
- C#代码 json类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- bug fix: openstack can not run swift for pyeclib and liberasurecode do not match
最近在使用devstack 安装openstack nimble项目. nimble项目是一个专业的baremetal管理项目. 安装过程中,遇到这个问题. /opt/stack/swift/bin/ ...
- Android 菜单(OptionMenu)大全 建立你自己的菜单
转自:http://www.cnblogs.com/salam/archive/2011/04/04/2005329.html 菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单 ...
- 关于AndroidManifest.xml
一.关于AndroidManifest.xml http://themeforest.net/item/metro-vibes-showcase-html-theme/full_screen_prev ...
- css系列教程--color direction line-height letter-spacing
css标签:colorcolor:用法color:指定文本的颜色color:red/#fff/unicode; direction:用法 direction:定义文本的方向.dirction:ltr/ ...
- asp.net几种<% %>用法
在asp.net应用程序中,在asp.net页面常用的<%@ %>.<%# %>.<%= %>.在全球化的项目中使用<%$ %>绑定资源项目,在asp. ...