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 <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][n][1][n]<<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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值