博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4597 Play Game 记忆化DP
阅读量:6259 次
发布时间:2019-06-22

本文共 2456 字,大约阅读时间需要 8 分钟。

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 a
i (1≤a
i≤10000). The third line contains N integer b
i (1≤b
i≤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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std ;int a[28] ,b[28] ;int suma[28] ,sumb[28] ;int dp[28][28][28][28] ;int N ;int dfs(int U , int B ,int L ,int R){ if(dp[U][B][L][R] != -1) return dp[U][B][L][R] ; if(U > B && L > R) return dp[U][B][L][R] = 0 ; int sum = 0 ; if(U <= B) sum += suma[B] - suma[U-1] ; if(L <= R) sum += sumb[R] - sumb[L-1] ; int nowstate = 0 ; if(U == B) nowstate = Max(nowstate , sum - dfs(U+1,B-1,L,R)) ; else if(U < B){ nowstate = Max(nowstate , sum - dfs(U+1,B,L,R)) ; nowstate = Max(nowstate , sum - dfs(U,B-1,L,R)) ; } if(L == R) nowstate = Max(nowstate , sum - dfs(U,B,L+1,R-1)) ; else if(L < R){ nowstate = Max(nowstate , sum - dfs(U,B,L+1,R)) ; nowstate = Max(nowstate , sum - dfs(U,B,L,R-1)) ; } return dp[U][B][L][R] = nowstate ;}int main(){ int T ; scanf("%d",&T) ; while(T--){ scanf("%d",&N) ; suma[0] = sumb[0] = 0 ; for(int i = 1 ; i <= N ; i++){ scanf("%d",&a[i]) ; suma[i] = suma[i-1] + a[i] ; } for(int i = 1 ; i <= N ; i++){ scanf("%d",&b[i]) ; sumb[i] = sumb[i-1] + b[i] ; } memset(dp,-1,sizeof(dp)) ; printf("%d\n",dfs(1,N,1,N)) ; } return 0 ;}

 

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3462513.html

你可能感兴趣的文章
深入浅出讲解:php的socket通信
查看>>
Photoshop 批量处理图片
查看>>
浅谈C# 多态的魅力(虚方法,抽象,接口实现)
查看>>
jQuery--百度百科
查看>>
Unity3D 之2D动画机
查看>>
基础知识系列☞闲言
查看>>
蓝牙Ibeacon室内定位和微信摇一摇周边原理分析
查看>>
架构设计:负载均衡层设计方案(7)——LVS + Keepalived + Nginx安装及配置
查看>>
virtualbox端口转发
查看>>
DiscuzX2.5 程序底层架构
查看>>
Jenkins_多项目构建(二):使用Maven聚集关系
查看>>
三大做空工具详解
查看>>
linux全方位掌握一个命令--思路比方法更重要
查看>>
[Flexbox] Use Flex to Scale Background Image
查看>>
【等待事件】序列等待事件总结(enq: SQ - contention、row cache lock、DFS lock handle和enq: SV - contention)...
查看>>
算法与数据结构(七) AOV网的拓扑排序(Swift版)
查看>>
maven pom.xml解释 (转)
查看>>
markdown to html
查看>>
Pspice仿真器
查看>>
ogg 、 Shareplex和DSG RealSync 对比
查看>>