HDU-4597 Play Game (区间DP)
2016-05-01 00:17
281 查看
Play Game
http://acm.hdu.edu.cn/showproblem.php?pid=4597Time 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 23 53 3 10 100 20 2 4 3
Sample Output
53 105
题目大意:给定两堆均含有n张牌的牌堆,牌上的数字分别是a[i],b[i],每次都已从任意一堆的顶部或底部拿一张,每个人都用最优策略,求先手能拿到的最大数字和?
设dp[i][j][k][l]表示先手从a数组区间[i,j]中以及b数组区间[k,l]中能选得的最大数字和
则dp[i][j][k][l]可由四个状态转移而来:
①先手拿a[i],由dp[i+1][j][k][l]转移而来,即 a[i]+sum(i+1,j,k,l)-dp[i+1][j][k][l];
②先手拿a[j],由dp[i][j-1][k][l]转移而来,即 a[j]+sum(i,j-1,k,l)-dp[i][j-1][k][l];
③先手拿b[k],由dp[i][j][k+1][l]转移而来,即 b[k]+sum(i,j,k+1,l)-dp[i][j][k+1][l];
④先手拿b[l],由dp[i][j][k][l-1]转移而来,即 b[l]+sum(i,j,k,l-1)-dp[i][j][k][l-1];
则dp[i][j][k][l]为上述最大值
感觉和只有一堆牌的情况差不多,但是连状态如何转移都知道了,却不知道如何实现,因为存在一堆没有选,而另一堆已选了的状态(这些状态没法表示,不好控制边界),看了题解才知道要用dfs控制边界从而进行状态的转移
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN=10005; int par[MAXN],ps,pe,n,m,a[25],b[25]; int dp[25][25][25][25];//dp[i][j][k][l]表示先手从a数组区间[i,j]中以及b数组区间[k,l]中能选得的最大数字和 int dfs(int i,int j,int k,int l) { if(i>j&&k>l) { return 0; } if(dp[i][j][k][l]!=0) { return dp[i][j][k][l]; } int mx=0,sum=0; if(i<=j) { sum+=a[j]-a[i-1]; } if(k<=l) { sum+=b[l]-b[k-1]; } if(i<=j) { mx=max(mx,sum-dfs(i+1,j,k,l)); mx=max(mx,sum-dfs(i,j-1,k,l)); } if(k<=l) { mx=max(mx,sum-dfs(i,j,k+1,l)); mx=max(mx,sum-dfs(i,j,k,l-1)); } return dp[i][j][k][l]=mx; } int main(){ int T; scanf("%d",&T); while(T-->0) { a[0]=b[0]=0; memset(dp,0,sizeof(dp)); scanf("%d",&n); for(int i=1;i<=n;++i) { scanf("%d",a+i); a[i]+=a[i-1]; } for(int i=1;i<=n;++i) { scanf("%d",b+i); b[i]+=b[i-1]; } printf("%d\n",dfs(1,n,1,n)); } return 0; }
相关文章推荐
- google搜索引擎核心PageRank
- 使用Storyboard 创建ViewController
- Linux基础(1)--学习环境搭建--RHEL6.5
- Spark学习三:Spark Schedule以及idea的安装和导入源码
- Spark学习四:网站日志分析案例
- zabbix3.0 监控mysql服务器性能实现过程
- HDU 1542 Atlantis
- AFN
- htoi(十六进制转十进制)
- (Spring加载xml时)org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
- Lab4: bootloader
- ivy resolve标签
- 《clip+》为你的聊天打上马赛克
- POJ_1084_SquareDestroyer(DancingLinksX重复覆盖)
- pathspec did not match any file known to git
- iOS边练边学--AFNetWorking框架GET、Post、Download、Upload,数据解析模式以及监控联网状态
- readLine方法的简单练习,回答2016-04-27 的帖子的问题
- L1 L2正则化
- Java基础串理
- 知识联结梳理 : I/O多路复用、EPOLL(SELECT/POLL)、NIO、Event-driven、Reactor模式