读书人

hdu4597之记忆化搜寻

发布时间: 2013-09-15 19:58:13 作者: rapoo

hdu4597之记忆化搜索

Play GameTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 259 Accepted Submission(s): 164


Problem DescriptionAlice 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?
InputThe 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).
OutputFor 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;}

读书人网 >其他相关

热点推荐