求解,我这种写法难道不是O(n^2)吗?怎么会re

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

CY666 @ 2020-12-15 16:54:16

#include<bits/stdc++.h>
using namespace std;
const int MAX=101;
int d[MAX][MAX];
int n;
int maxsum[MAX][MAX];
int Maxsum(int i,int j)
{
    if(maxsum[i][j]!=-1)
    {
        return maxsum[i][j];
    }
    if(i==n)
    {
        maxsum[i][j]=d[i][j];
    }
    else
    {
        int x=Maxsum(i+1,j);
        int y=Maxsum(i+1,j+1);
        maxsum[i][j]=max(x,y)+d[i][j];
    }
    return maxsum[i][j];
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            cin>>d[i][j];
            maxsum[i][j]=-1;
        }
    }
    cout<<Maxsum(1,1)<<endl;
    return 0;
}

by 滑大稽 @ 2020-12-15 16:56:57

数组开小了,TLE!=RE


by Miquella @ 2021-01-26 20:17:12

数组至少开1001


|