Luogu P1169_ Suspended line method to find the maximum matrix area

Find the area of ​​the largest x matrix

h[maxn][maxn] records the position where the position extends up the most

Initialization: Line 0 is all set to 0

更新: h[i][j] = a[i][j]==x?h[i - 1][j]+1:0;

l[maxn][maxn], r[maxn][maxn]; record the first left/right to reach the position that is not x

tmp records the column number of the last position that is not x

Initialization: Set the 0th row of l[][] to 0, and the 0th row of r[][] to all set to m+1

Update: If the current position is x, then l[i][j] = max(l[i - 1][j], tmp), r[i][j] = min(r[i - 1][j ], tmp)

           On the contrary, set tmp to the column number of the position, the value of this position is set to l[i][j] = 0, r[i][j] = m + 1 (in order not to affect the following lines)

*********************************************************************************************************************

Area calculation, the location:

If it is the largest matrix: (r[i][j]-l[i][j]-1) * h[i][j]

Wakashi maximum square: min (r [i] [j]-l [i] [j] -1, h [i] [j]) *  min (r [i] [j]-l [i] [j] -1, h [i] [j])

*********************************************************************************************************************

#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 2010;

int a[maxn][maxn];
int h[maxn][maxn], l[maxn][maxn], r[maxn][maxn];

int n, m;
int ans1, ans2;

void solve(bool x);//Find the largest x matrix

intmain()
{
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
    {
        scanf("%d", &a[i][j]);
        if((i&1)!=(j&1)) a[i][j] = !a[i][j];//Transform the problem according to this method
    }
    ans1 = ans2 = 0;
    solve(1);
    solve(0);
    printf("%d\n%d\n", ans1, ans2);
    return 0;
}

void solve(bool x)
{
    //initialization
    int i, j;
    for(i = 0; i <= m; ++i)
    {
        h[0][i] = 0;
        l[0][i] = 0;
        r[0][i] = m + 1;
    }

    for(i = 1; i <= n; ++i)
    {
        for(j = 1; j <= m; ++j) h[i][j] = a[i][j]==x?h[i - 1][j]+1:0;
        int tmp = 0;//Initially, it is considered that the first position to the left that is not x is 0;
        for(j = 1; j <= m; ++j)
            if(a[i][j]==x) l[i][j] = max(l[i - 1][j], tmp);
            else l[i][j] = 0, tmp = j;//The l[i][j]=0 that does not satisfy the situation will not affect the next line
        tmp = m + 1;//Initially think that the first position to the right that is not x is m+1;
        for(j = m; j; --j)
            if(a[i][j]==x) r[i][j] = min(r[i - 1][j], tmp);
            else r[i][j] = m + 1, tmp = j;
        for(j = 1; j <= m; ++j)
        {
            int tmp = r[i][j]-l[i][j]-1;
            ans2 = max(ans2, tmp*h[i][j]);
            int minn = min (tmp, h [i] [j]);
            ans1 = max (ans1, from * from);
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726282&siteId=291194637