两种几乎一样的代码,一种全超时,一种全AC

P1736 创意吃鱼法

SSDUT_Wxh @ 2020-02-12 13:43:09

#include<iostream>
using namespace std;
int dp[3000][3000],arr1[3000][3000],arr2[3000][30000],ans;
int map[3000][3000],n,m;
long long min(long long a, long long b)
{
    return a < b ? a : b;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            cin >> map[i][j];
            if (!map[i][j])
            {
                arr1[i][j] = arr1[i][j - 1] + 1;
                arr2[i][j] = arr2[i - 1][j] + 1;
            }
            else
            {
                dp[i][j] = dp[i][j] = min(min(arr1[i][j - 1], arr2[i - 1][j]), dp[i - 1][j - 1]) + 1;
                if (ans < dp[i][j])ans = dp[i][j];
            }
        }
    memset(dp, 0, sizeof(dp));
    memset(arr1, 0, sizeof(arr1));
    memset(arr2, 0, sizeof(arr2));
    for (int i = n; i >= 1; --i)
        for (int j = 1; j <= m; ++j)
        {
            if (!map[i][j])
            {
                arr1[i][j] = arr1[i + 1][j] + 1;
                arr2[i][j] = arr2[i][j - 1] + 1;
            }
            else
            {
                dp[i][j] = min(min(arr1[i + 1][j], arr2[i][j - 1]), dp[i + 1][j + 1]) + 1;
                if (dp[i][j] > ans) ans = dp[i][j];
            }
        }
    cout << ans << endl;
    return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
typedef long long ll;
using namespace std;
int n,m,ans;
int a[2509][2509],f[2509][2509],s1[2509][2509],s2[2509][2509];//s1为横向,s2为纵向 
int main()
{
    cin>>n>>m;
    //第一遍左上——右下 
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    {
        scanf("%d",&a[i][j]);
        if(!a[i][j])
        {
            s1[i][j]=s1[i][j-1]+1;
            s2[i][j]=s2[i-1][j]+1;
        }
        if(a[i][j])
        f[i][j]=min(f[i-1][j-1],min(s1[i][j-1],s2[i-1][j]))+1;
        ans=max(ans,f[i][j]);
    }
    //第二遍右上——左下 
    memset(f,0,sizeof(f)); 
    memset(s1,0,sizeof(s1));//数组置0 
    memset(s2,0,sizeof(s2)); 
    for(int i=1;i<=n;i++)
    for(int j=m;j>=1;j--)
    {
        if(!a[i][j])
        {
            s1[i][j]=s1[i][j+1]+1;
            s2[i][j]=s2[i-1][j]+1;
        }
        if(a[i][j])
        f[i][j]=min(f[i-1][j+1],min(s1[i][j+1],s2[i-1][j]))+1;
        ans=max(ans,f[i][j]);
    }
    cout<<ans<<endl;
    return 0;
}

by pocafup @ 2020-02-12 13:54:44

哪个t了


by schtonn @ 2020-02-12 13:54:54

UltraCompare


by SSDUT_Wxh @ 2020-02-13 11:17:57

@pocafup 第一个


by SSDUT_Wxh @ 2020-02-13 11:19:22

@l_x_l 菜鸡看不懂


by pocafup @ 2020-02-13 11:57:53

arr2[3000][30000]

@DUTwxh


by pocafup @ 2020-02-13 11:58:13

你不t谁t


|