hdu4597之记忆化搜索
来源:互联网 发布:逆战一键瞬狙宏数据 编辑:程序博客网 时间:2024/05/01 22:21
Play Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 259 Accepted Submission(s): 164
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).
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#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=20+10;int dp[MAX][MAX][MAX][MAX],s1[MAX],s2[MAX],sum1[MAX],sum2[MAX];//dp[a][b][i][j]表示当前玩家从s1的a~b,s2的i~j能获得的最大价值 int dfs(int a,int b,int i,int j){ if(dp[a][b][i][j])return dp[a][b][i][j]; if(a>b && i>j)return 0; int max1=0,max2=0; if(a<=b)max1=max(s1[a]+dfs(a+1,b,i,j),s1[b]+dfs(a,b-1,i,j)); if(i<=j)max2=max(s2[i]+dfs(a,b,i+1,j),s2[j]+dfs(a,b,i,j-1)); dp[a][b][i][j]=sum1[b]-sum1[a-1]+sum2[j]-sum2[i-1]-max(max1,max2); return dp[a][b][i][j];}int main(){ int t,n; cin>>t; while(t--){ int ans=0; scanf("%d",&n); for(int i=1;i<=n;++i)cin>>s1[i],sum1[i]=sum1[i-1]+s1[i]; for(int i=1;i<=n;++i)cin>>s2[i],sum2[i]=sum2[i-1]+s2[i]; memset(dp,0,sizeof dp); printf("%d\n",sum1[n]+sum2[n]-dfs(1,n,1,n)); } return 0;}
- hdu4597之记忆化搜索
- HDU4597:Play Game(记忆化搜索(dp))(博弈)
- HDU4597:Play Game(记忆化)
- 记忆化搜索之拍卖
- hdu4597
- 记忆化搜索之LCS实践
- 记忆化搜索算法之动态规划
- poj1088滑雪--dp之记忆化搜索
- 记忆化搜索之P1464 Function
- 动态规划之从搜索到记忆化搜索到递推式
- 记忆化搜索例题
- PKU1088---记忆化搜索
- PKU1088 记忆化搜索
- HDU1078 记忆化搜索
- HDU1501 记忆化搜索
- HDU1978 记忆化搜索
- poj1661 记忆化搜索
- zoj1107记忆化搜索
- 解决ArcCatalog 10.0中improt数据sde失败,Dataset Database Connections\Connection to 计算机名.sde does not exist
- Android State List
- The listener supports no services解决一例
- 段错误原因
- BZOJ 2243 树链剖分
- hdu4597之记忆化搜索
- Linux下ps命令详解
- 自旋锁与信号量的区别
- VMware 下ubuntu的安装存在的一些问题
- java基础常识
- github (linux)
- 小心笔试中的小陷阱(四)
- C# 随机函数Random 半翻
- vim中跨文件的多行拷贝