hdu-4597 Play Game(区间DP)

Play Game

点我找原题
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 888    Accepted Submission(s): 519


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 23 53 3 10 100 20 2 4 3
 

Sample Output
 
    
53 105
 


啃了三个小时的题,太笨了,即使是一次AC。
参考博客: http://blog.csdn.net/shuangde800/article/details/10277697     思路是参考的,可是大神们都能用DFS直接解决,我却只能一点点用转移方程求出来,说多了都是泪。

 
    

#include
#include
#include
#include
#include
using namespace std;
int t,n,a[22],b[22],i,j,k,l;
int dp[22][22][22][22],f1[22][22],f2[22][22];
int sum1[22],sum2[22];
int minn(int i,int j,int k,int l)
{
int ans;
int a,b,c,d;
a=dp[i+1][j][k][l];
b=dp[i][j-1][k][l];
c=dp[i][j][k+1][l];
d=dp[i][j][k][l-1];
if(i==j)
{
a=f2[k][l];
b=f2[k][l];
}
if(k==l)
{
c=f1[i][j];
d=f1[i][j];
}
ans=min(a,b);
ans=min(ans,c);
ans=min(ans,d);
return ans;
}
int main(int argc, char *argv[])
{
cin>>t;
while(t--)
{
cin>>n;
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
cin>>a[i];
sum1[i]=sum1[i-1]+a[i];
}
for(i=1;i<=n;i++)
{
cin>>b[i];
sum2[i]=sum2[i-1]+b[i];
}
for(i=n;i>0;i--){
for(j=i;j<=n;j++){
if(i==j) f1[i][j]=a[i];
else {
f1[i][j]=sum1[j]-sum1[i-1]-min(f1[i+1][j],f1[i][j-1]);
}
}
}
for(i=n;i>0;i--){
for(j=i;j<=n;j++){
if(i==j) f2[i][j]=b[i];
else {
f2[i][j]=sum2[j]-sum2[i-1]-min(f2[i+1][j],f2[i][j-1]);
}
}
}
for(i=n;i>0;i--){
for(j=i-1;j<=n;j++){
for(k=n;k>0;k--){
for(l=k-1;l<=n;l++){
if(j==i-1&&l==k-1) continue;
else if(j==i-1) dp[i][j][k][l]=f2[k][l];
else if(l==k-1) dp[i][i][k][l]=f1[i][j];
else dp[i][j][k][l]=(sum1[j]-sum1[i-1]+sum2[l]-sum2[k-1])-
minn(i,j,k,l);

}
}
}
}
cout< /*for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
cout< }
}
cout< for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
cout< }
}
cout< }
return 0;
}



你可能感兴趣的:(动态规划)