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. Android 查看內存使用

    一.使用dumpsys meminfo命令 1.使用dumpsys meminfo查看内存使用情况 2.过滤某个进程可以使用 dumpsys meminfo | grep -i phone 二,使用t ...

  2. 史上最全Java表单验证封装类

    package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...

  3. phonegap–app启动欢迎引导页localstorage

    对一个新的app,一般情况都会添加一个介绍和欢迎的页面来告诉用户app的功能和新的特性. 那么在phonegap项目里面如何添加这样个引导欢迎页. 这里需要注意的是只有app第一次打开的时候才会有,其 ...

  4. 支付宝客户端支付配置RSA公钥的问题错误,导致收不到回发通知

    没收到通知的原因是你们的商户公钥上传地址弄错了,应该上传到合作伙伴管理,您上传到无线wap哪里了,把您的公钥,从无线wap哪里复制贴到合作伙伴管理即可

  5. c# 中的多线程和异步

    前言: 1.异步和多线程有区别吗? 答案:多线程可以说是实现异步的一种方法方法,两者的共同目的:使主线程保持对用户操作的实时响应,如点击.拖拽.输入字符等.使主程序看起来实时都保持着等待用户响应的状态 ...

  6. Learning Puppet — Manifests

    Begin In a text editor — vim, emacs, or nano — create a file with the following contents and filenam ...

  7. HttpUrlConnection java.net.SocketException: Software caused connection abort: recv failed

    最近做java swing程序在模拟httprequest请求的时候出现了这个错误 java.net.SocketException: Software caused connection abort ...

  8. 探秘JavaScript中的六个字符

    JavaScript 是一个奇怪而有趣的语言,我们可以写一些疯狂却仍然有效的代码.它试图帮助我们把事情转换到基于我们如何对待他们的特定类型. 如果我们添加一个字符串,JavaScript会假定我们希望 ...

  9. Existence and nonexistence results for anisotropic quasilinear elliptic equations

    Fragalà, Ilaria; Gazzola, Filippo; Kawohl, Bernd. Existence and nonexistence results for anisotropic ...

  10. 优化studio的速度

    随着Android Studio开发工具的逐渐成熟,越来越多的程序员选择这种IDE工具来进行开发,但是android studio在使用过程中有时候会出现卡顿问题.在赶项目的时候,遇到这类问题最是苦恼 ...