题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597

题目大意:

有两行卡片,每个卡片都有各自的权值。

两个人轮流取卡片,每次只能从任一行的左端或右端取卡片。

假设两人都足够聪明,求先手能够取到的最大权值之和。

解题思路:

这题就归为区间DP吧,设dp[l1][r1][l2][r2]表示取完第一行[l1,r1]和第二行[l2,r2]的卡片时,先手能够获得的最大权值。

那么跟(l1,r1,l2,r2)关联的区间只有四个:

(l1+1,r1,l2,r2)

(l1,r1-1,l2,r2)

(l1,r1,l2+1,r2)

(l1,r1,l2,r2-1)

那么状态转移方程就为:

dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-solve(l1+1,r1,l2,r2)),(l1<=r1)
dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-solve(l1,r1-1,l2,r2)),(l1<=r1)

dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-solve(l1,r1,l2+1,r2)),(l2<=r2)
dp[l1][r1][l2][r2]=max(dp[l1][r1][l2][r2],sum-solve(l1,r1,l2,r2-1)),(l2<=r2)

其中sum为第一行[l1,r1]和第二行[l2,r2]卡片权值之和,

从子区间推过来相当于先后手顺序发生了改变,所以sum-子区间最优解相当于取反,就是原来先手的操作变成后手,后手变成先手。

那么我们为什么要从最优子区间解里推过来呢,那样不就变成了让后手有了较优解了吗,不就让先手获得的权值变少了吗?因为题目说了两人足够聪明,选取的都是最优解。

代码:

 #include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r1",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=;
const int INF=0x3f3f3f3f;
const double eps=1e-; int a[N],b[N];
int dp[N][N][N][N]; int solve(int l1,int r1,int l2,int r2){
if(dp[l1][r1][l2][r2]!=-)
return dp[l1][r1][l2][r2]; int ans=,sum=;
if(l1<=r1)
sum+=a[r1]-a[l1-];
if(l2<=r2)
sum+=b[r2]-b[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(){
FAST_IO;
int t;
scanf("%d",&t);
while(t--){
memset(dp,-,sizeof(dp));
int n;
scanf("%d",&n);
_for(i,,n){
cin>>a[i];
a[i]+=a[i-];
}
_for(i,,n){
cin>>b[i];
b[i]+=b[i-];
}
printf("%d\n",solve(,n,,n));
}
return ;
}

HDU 4597 Play Game(区间DP(记忆化搜索))的更多相关文章

  1. hdu 4597 Play Game(区间dp,记忆化搜索)

    Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...

  2. (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)

    http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats ...

  3. UVA 10003 Cutting Sticks 区间DP+记忆化搜索

    UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...

  4. uva 10891 区间dp+记忆化搜索

    https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...

  5. 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 ...

  6. HDU - 6143 Killer Names(dp记忆化搜索+组合数)

    题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的 ...

  7. HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索

    题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析:  枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...

  8. loj 1031(区间dp+记忆化搜索)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...

  9. BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】

    题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...

  10. HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)

    题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...

随机推荐

  1. 关于maven工程打jar的问题

    今天对maven做一些整理,更好的理了下思路: 这个篇博客介绍的还是很详细的: http://www.cnblogs.com/haippy/archive/2012/07/04/2576453.htm ...

  2. js 字符串格式化

    在js 文件中写一个函数 String.prototype.format = function(args) { var result = this; if (arguments.length > ...

  3. python——type()、metaclass元类和精简ORM框架

    1.type()函数 if __name__ == '__main__': h = hello() h.hello() print(type(hello)) print(type(h)) Hello, ...

  4. python---基础知识回顾(九)图形用户界面-------WxPython

    主要使用wxPython(最成熟的跨平台python GUI工具包) wxPython手册 前戏:基础了解 import wx class MyFrame(wx.Frame): #创建自定义Frame ...

  5. python---CRM用户关系管理

    Day1:项目分析 一:需求分析 二:CRM角色功能介绍 三:业务场景分析 销售: .销售A 从百度推广获取了一个客户,录入了CRM系统,咨询了Python课程,但是没有报名 .销售B 从qq群获取一 ...

  6. 【数据库-MySql】清空所有表格的所有数据

    方式一. drop procedure if exists del_all_tb; delimiter $$ create procedure del_all_tb(db char(20)) begi ...

  7. 戴尔PowerEdge R430 机架式服务器 安装ubuntu server 14.04.1 LTS 64 位

    硬件配置: 服务编号:5Z04X72 软件配置 1.Ubuntu 系统下载地址: https://certification.ubuntu.com/certification/hardware/201 ...

  8. Machine Learning Trick of the Day (1): Replica Trick

    Machine Learning Trick of the Day (1): Replica Trick 'Tricks' of all sorts are used throughout machi ...

  9. Is it possible to create @Around Aspect for feign.Client

    https://github.com/spring-cloud/spring-cloud-openfeign/issues/59 看到github 有人问这个问题,做了个示例: import org. ...

  10. JavaScript 运行机制之执行顺序详解

    JavaScript是一种描述型脚本语言,它不同于 Java 或 C# 等编译性语言,它不需要进行编译成中间语言,而是由浏览器进行动态地解析与执行.如果你不能理解 JavaScript 语言的运行机制 ...