HDU-4597-区间DP-(博弈)

代码特别好理解,看完代码后再脑中的镜像和模模糊糊的感性使我快速的写完了。。。。
如果知识是这样轻易获得的就好了。。。
小a和小b在两个并列的数组中取数,每次都能取最大的。可以从两个的各首和尾。
小a天命所归先取。
用记忆化搜索。DP[A][B][C][D]表示的是在1数组 A-B,2数组C-D的情况下,小a获取的最大值。
(对边界的处理不错)
还有就是理解:我每次的最大值都是在别人取了一个之后的最大值。
(猜测他取了哪个)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=30;
int dp[maxn][maxn][maxn][maxn];
int sum1[maxn];
int sum2[maxn];
int dfs(int a1,int a2,int b1,int b2)
{ int &ans=dp[a1][a2][b1][b2];
int now;//现在剩的数
  if(a1>a2)
{   now=sum2[b2]-sum2[b1-1];
     if(b1==b2)
     { ans=now;return ans;
     }
}
  else if(b1>b2)
{  now=sum1[a2]-sum1[a1-1];
    if(a1==a2)
    {  ans=now;return ans;
    }
}
else
    now=sum1[a2]-sum1[a1-1]+sum2[b2]-sum2[b1-1];
   if(dp[a1][a2][b1][b2]!=-1) return dp[a1][a2][b1][b2];
   if(a1<=a2)
    ans=max(ans,now-min(dfs(a1+1,a2,b1,b2),dfs(a1,a2-1,b1,b2)));
   if(b1<=b2)
   ans=max(ans,now-min(dfs(a1,a2,b1+1,b2),dfs(a1,a2,b1,b2-1)));
   cout<<ans<<"!"<<a1<<" "<<a2<<" "<<b1<<" "<<b2<<endl;
   return ans;
}
int main()
{  std::ios::sync_with_stdio(false);
    int m;
  int a,b;
  int t;
    cin>>t;
    while(t--)
    {  cin>>m;
      memset(dp,-1,sizeof(dp));
      sum1[0]=0;
      sum2[0]=0;
      for(int i=1;i<=m;i++)
         {cin>>a;
         sum1[i]=sum1[i-1]+a;
         }
      for(int i=1;i<=m;i++)
        {  cin>>b;
            sum2[i]=sum2[i-1]+b;
        }
        cout<<dfs(1,m,1,m)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值