• Largest Submatrix of All 1’s


    Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

    Input

    The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

    Output

    For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

    Sample Input
    2 2
    0 0
    0 0
    4 4
    0 0 0 0
    0 1 1 0
    0 1 1 0
    0 0 0 0
    Sample Output
    0
    4


    单调栈,每一行记录个列的高度 然后每一行进行单调栈,用输入输出流还是超时得用scanf。。或者 用scanf就不错。。


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <queue>
    #include <cmath>
    using namespace std;
    int n,m,d;
    int mp[2002][2002];
    int l[2002]={1},r[2002],stack[2001],top,maxi,sum;
    int main()
    {
        //ios::sync_with_stdio(false);
        //cin.tie(0);
    
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            maxi=0;
            for(int i=1;i<=n;i++)
            {
                top=1;
                for(int j=1;j<=m;j++)
                {
                    scanf("%d",&d);
                    //mp[i][j]=mp[i][j-1]+d;  不对的 中间如果有0 应该中断
                    mp[i][j]=(d==0?0:(mp[i-1][j]+d));
                    l[j]=r[j]=j;
                }
                for(int j=1;j<=m+1;j++)
                {
                    while(top>1&&mp[i][j]<=mp[i][stack[top-1]])
                    {
                        r[stack[top-1]]=j-1;
                        sum=(r[stack[top-1]]-l[stack[top-1]]+1)*mp[i][stack[top-1]];
                        if(sum>maxi)maxi=sum;
                        top--;
                    }
                    l[j]=stack[top-1]+1;
                    stack[top++]=j;
                }
            }
    
            printf("%d
    ",maxi);
        }
    }
    /*
    4 4
    1 0 1 1
    0 1 1 1
    0 0 1 1
    0 1 1 1
    */
  • 相关阅读:
    inotify和rsync实现数据实时同步
    Powershell在相应的文件夹下用管理员模式打开
    LOJ6498「雅礼集训 2018 Day2」农民
    LOJ6502「雅礼集训 2018 Day4」Divide
    LOJ6501「雅礼集训 2018 Day4」Cube
    2021-10-11 杂题选听
    LOJ6507 「雅礼集训 2018 Day7」A
    LOJ6497「雅礼集训 2018 Day1」图
    CF103E Buying Sets
    CF266D BerDonalds(图的绝对中心)
  • 原文地址:https://www.cnblogs.com/8023spz/p/7239162.html
Copyright © 2020-2023  润新知