您的位置:首页 > 其它

hdu-4597 Play Game(区间DP)

2018-01-22 09:11 309 查看

Play Game

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


[align=left]Problem Description[/align]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? <
4000
/div>
[align=left]Input[/align]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). 
[align=left]Output[/align]For each case, output an integer, indicating the most score Alice can get. 
[align=left]Sample Input[/align]
2

1
23
53

3
10 100 20
2 4 3  
[align=left]Sample Output[/align]
53
105  

啃了三个小时的题,太笨了,即使是一次AC。参考博客:http://blog.csdn.net/shuangde800/article/details/10277697     思路是参考的,可是大神们都能用DFS直接解决,我却只能一点点用转移方程求出来,说多了都是泪。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
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<<dp[1]
[1]
<<endl;
/*for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
cout<<f1[i][j]<<" ";
}
}
cout<<endl;
for(i=1;i<=n;i++){
for(j=i;j<=n;j++){
cout<<f2[i][j]<<" ";
}
}
cout<<endl;*/
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: