Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Time complexity 467C码力时间复杂度_Time Complexity - Fatal编程技术网

Time complexity 467C码力时间复杂度

Time complexity 467C码力时间复杂度,time-complexity,Time Complexity,今天,我尝试了很多次来解决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) ret

今天,我尝试了很多次来解决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;
    }
#包括
typedef long-long-ll;
ll n,m,k,a[5000],结果=0,备忘录[5000][5000],总和[5000];
ll最大和(整数p,整数t)
{
如果(t==0)返回0;
如果(备忘录[p][t]!=-1)
返回备忘录[p][t];
lltp=0;
对于(inti=p+m;i+(t-1)*m-1>n>>m>>k;
对于(int i=0;i>a[i];

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

请帮助我@,这让我头痛@@@如果您能在此处粘贴更正的代码,那将非常好。这将消除所有歧义。我也做了同样的修改ake before..您可以使用此技术解决dp问题。