【问题标题】:467C-CodeForces Time Complexity467C-CodeForces 时间复杂度
【发布时间】:2017-01-31 18:15:11
【问题描述】:

今天,我尝试了很多次来解决 codeforces 中的一个问题,我使用了 dp 但仍然失败了 TL。你们能指出我的代码中的哪一部分需要很长时间吗?谢谢!

    #include<bits/stdc++.h>

    typedef long long ll;

    ll n,m,k,a[5000],result=0, memo[5000][5000], sum[5000];

    ll maxsum(int p,int t)
    {
        if (t==0) return 0;
        if (memo[p][t]!=-1)
            return memo[p][t];
        ll tp=0;
        for (int i=p+m; i+(t-1)*m-1<n; ++i)
            tp=std::max(tp,maxsum(i,t-1));
        memo[p][t]=tp+sum[p];
        return memo[p][t];
    }

    int main()
    {
        std::ios::sync_with_stdio(0);
        std::cin.tie(0);
        std::cin>>n>>m>>k;
        for (int i=0; i<n; ++i)
            std::cin>>a[i];
        for (int i=0; i<m; ++i)
            sum[0]+=a[i];
        for (int i=1; i+m-1<n; ++i)
            sum[i]=sum[i-1]+a[i+m-1]-a[i-1];
        std::fill(&memo[0][0],&memo[n][k],-1);
        for (int i=0; i+k*m-1<n; ++i)
            result=std::max(result,maxsum(i,k));
        std::cout<<result;
    }

我提交的链接在这里:http://codeforces.com/contest/467/submission/24322748

【问题讨论】:

  • 请帮帮我@@,这让我头疼@@@@@@

标签: time-complexity


【解决方案1】:

答案可能为零,因此您应该在开始时将动态数组的所有元素设置为 -1,然后在您的函数中检查它们是否等于 -1。

【讨论】:

  • 如果您能在此处粘贴更正的代码,那就太好了。这将消除所有的歧义。
  • 我之前也犯过同样的错误。 Here are my codes 。您可以将此技术用于 dp 问题。
猜你喜欢
  • 2016-02-13
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2018-08-02
  • 2014-04-29
相关资源
最近更新 更多