hdu 4597 Play Game(区间dp,记忆化搜索)

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≤), indicating the number of cases.
Each case contains lines. The first line is the N (N≤). The second line contains N integer ai (≤ai≤). The third line contains N integer bi (≤bi≤).
 
Output
For each case, output an integer, indicating the most score Alice can get.
Sample Input

 
Sample Output

 
Source
 
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int sum1[],sum2[];
int a[];
int b[];
int dp[][][][];
int dfs(int l1,int r1,int l2,int r2)
{
if(dp[l1][r1][l2][r2]!=-)
return dp[l1][r1][l2][r2];
//if(l1>r1 && l2>r2) return dp[l1][r1][l2][r2]=0;
int sum=;
int ans=;
if(l1<=r1)
{
sum+=sum1[r1]-sum1[l1-];
}
if(l2<=r2)
{
sum+=sum2[r2]-sum2[l2-];
}
if(l1<=r1)
{
ans=max(ans,sum-min(dfs(l1+,r1,l2,r2),dfs(l1,r1-,l2,r2)));
}
if(l2<=r2)
{
ans=max(ans,sum-min(dfs(l1,r1,l2+,r2),dfs(l1,r1,l2,r2-)));
}
return dp[l1][r1][l2][r2]=ans;
}
int main()
{
int t;
int n;
cin>>t;
while(t--)
{
cin>>n;
sum1[]=sum2[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum1[i]=sum1[i-]+a[i];
}
for(int i=;i<=n;i++)
{
scanf("%d",&b[i]);
sum2[i]=sum2[i-]+b[i];
}
memset(dp,-,sizeof(dp));
printf("%d\n",dfs(,n,,n));
}
return ;
}
上一篇:hdu 4745 Two Rabbits 区间DP


下一篇:jQuery mouseove和mouseout事件不断触发