Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

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
2

3
5
3
3
10 100
20
2 4 3
 
Sample Output
53
105
 
Source
 
 
   没有后效性,因为每一状态都是最优策略。
   确定状态的转移 。每个状态的数的和一定,一旦子问题取数和确定,则父节点取树和确定,枚举4中情况,取最大(优)。
   此题递归树性质:
          父亲节点father 的取数和 与  儿子节点son的取数和 为 定值 。这就是状态转移的关键地方 。 
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
int a[] ,b[] ;
int suma[] ,sumb[] ;
int dp[][][][] ;
int N ; int dfs(int U , int B ,int L ,int R){
if(dp[U][B][L][R] != -)
return dp[U][B][L][R] ;
if(U > B && L > R)
return dp[U][B][L][R] = ;
int sum = ;
if(U <= B)
sum += suma[B] - suma[U-] ;
if(L <= R)
sum += sumb[R] - sumb[L-] ;
int nowstate = ;
if(U == B)
nowstate = Max(nowstate , sum - dfs(U+,B-,L,R)) ;
else if(U < B){
nowstate = Max(nowstate , sum - dfs(U+,B,L,R)) ;
nowstate = Max(nowstate , sum - dfs(U,B-,L,R)) ;
}
if(L == R)
nowstate = Max(nowstate , sum - dfs(U,B,L+,R-)) ;
else if(L < R){
nowstate = Max(nowstate , sum - dfs(U,B,L+,R)) ;
nowstate = Max(nowstate , sum - dfs(U,B,L,R-)) ;
}
return dp[U][B][L][R] = nowstate ;
} int main(){
int T ;
scanf("%d",&T) ;
while(T--){
scanf("%d",&N) ;
suma[] = sumb[] = ;
for(int i = ; i <= N ; i++){
scanf("%d",&a[i]) ;
suma[i] = suma[i-] + a[i] ;
}
for(int i = ; i <= N ; i++){
scanf("%d",&b[i]) ;
sumb[i] = sumb[i-] + b[i] ;
}
memset(dp,-,sizeof(dp)) ;
printf("%d\n",dfs(,N,,N)) ;
}
return ;
}
 

HDU 4597 Play Game 记忆化DP的更多相关文章

  1. HDU 4597 Play Game (记忆化搜索博弈DP)

    题意 给出2*n个数,分两列放置,每列n个,现在alice和bob两个人依次从任意一列的对头或队尾哪一个数,alice先拿,且两个人都想拿最多,问alice最后能拿到数字总和的最大值是多少. 思路 4 ...

  2. hdu 4597 Play Game(记忆化搜索)

    题目链接:hdu 4597 Play Game 题目大意:给出两堆牌,仅仅能从最上和最下取,然后两个人轮流取,都依照自己最优的策略.问说第一个人对多的分值. 解题思路:记忆化搜索,状态出来就很水,dp ...

  3. HDU 4597 Play Game(记忆化搜索,深搜)

    题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...

  4. Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

    Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...

  5. UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

    题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...

  6. cf835(预处理 + 记忆化dp)

    题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...

  7. cf779D(记忆化dp)

    题目链接: http://codeforces.com/problemset/problem/799/D 题意: 给出两个矩阵边长 a, b, 和 w, h, 以及一个 c 数组, 可选择 c 数组中 ...

  8. Codeforces1107E Vasya and Binary String 记忆化dp

    Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...

  9. POJ 1088 滑雪(简单的记忆化dp)

    题目 又一道可以称之为dp的题目,虽然看了别人的代码,但是我的代码写的还是很挫,,,,,, //看了题解做的简单的记忆化dp #include<stdio.h> #include<a ...

随机推荐

  1. jquery导航动画

    经常在网上看到的,鼠标在导航上移动时,导航底部的横条会自动移动到鼠标悬浮的导航项上. 效果如下图: 利用jquery的 animate 函数,很好实现.代码很简单! 代码如下: <!DOCTYP ...

  2. nova分析(4)—— nova-cells

    cells的功能允许用户以更分散的方式去扩展OpenStack计算云而无需使用复杂的技术,比如数据库和消息队列的集群.它的目的是支持非常大规模的部署. 当启用了此功能,在OpenStack计算云中的主 ...

  3. CentOS 7.0系统安装配置图解教程

    转自:http://www.osyunwei.com/archives/7829.html 操作系统:CentOS 7.0 64位 IP地址:192.168.21.128 网关:192.168.21. ...

  4. Redis容灾部署(哨兵Sentinel)

    Redis容灾部署(哨兵Sentinel) 哨兵的作用 1. 监控:监控主从是否正常2. 通知:出现问题时,可以通知相关人员3. 故障迁移:自动主从切换4. 统一的配置管理:连接者询问sentinel ...

  5. 【转】JVM 分代GC策略分析

    我们以Sun HotSpot VM来进行分析,首先应该知道,如果我们没有指定任何GC策略的时候,JVM默认使用的GC策略.Java虚拟机是按照分代的方式来回收垃圾空间,我们应该知道,垃圾回收主要是针对 ...

  6. Axure RP

    Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写. Axure RP是美国Axu ...

  7. Keepalived高可用软件的安装与配置

    监听和替换多台服务器之间的来回切换 一.安装tar zxvf keepalived-1.1.15.tar.gzcd keepalived-1.1.15./configure --prefix=/usr ...

  8. sql语句(mysql)

    MySQL1.数据库数据类型 int 整型 double 浮点型 如double(5,2)最多5位,其中有两位小数 char:固定长度字符串,如char(255),数据长度不足会用空格补足 varch ...

  9. python (11)文件的读写 按行读文件

    读文件: 读取文件 f = open('\info.txt') fil = f.read() f.close() 按行读文件: f = open("info.txt") while ...

  10. 《精通SQL Server 2008》笔记

    7.3触发器的使用 7.4游标的使用 8索引/关系图/完整性 9用户管理