• 2906: 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.

    输入

    The input contains multiple test cases. Each test case begins with m and n (1 ≤ mn ≤ 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.

    输出

     

    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.

    样例输入

     

    2 2
    0 0
    0 0
    4 4
    0 0 0 0
    0 1 1 0
    0 1 1 0
    0 0 0 0

    样例输出

     

    0
    4

    高:是1累加上前一行的,0的话就是0;

    长:把每一行作为长,算mp【i】【j】的时候分别向左向右找第一个比它小的数,所以用单调递减栈。

    这有个讲单调栈和单调队列的讲的超级好,我觉得(https://www.cnblogs.com/tham/p/8038828.html 

    #include<bits/stdc++.h>
    using namespace std;
    int mp[2222][2222];
    int l[2222],r[2222];
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=1; i<=n; i++)
            {
                for(int j=1; j<=m; j++)
                {
                    scanf("%d",&mp[i][j]);
                    if(mp[i][j]) mp[i][j]+=mp[i-1][j];
                }
            }
            for(int i=1; i<=n; i++)
            {
                mp[i][0]=-1;
                mp[i][m+1]=-1;
            }
            int mx=0;
            stack<int>s;
            while(!s.empty())s.pop();
            for(int i=1; i<=n; i++)
            {
                while(!s.empty())s.pop();
                s.push(0);
                for(int j=1; j<=m; j++)//在前面找第一个比自己小的 
                {
                    while(!s.empty()&&mp[i][j]<=mp[i][s.top()])s.pop();//当前元素小于等于栈顶  出栈 
                    l[j]=s.top();//记录下标 
                    s.push(j);
                }
                while(!s.empty())s.pop();
                s.push(m+1);
                for(int j=m; j>=1; j--)//在后面找第一个比自己小的 
                {
                    while(!s.empty()&&mp[i][j]<=mp[i][s.top()])s.pop();
                    r[j]=s.top(); 
                    s.push(j);
                }
                int sum=0;
                for(int j=1; j<=m; j++)
                {
                    sum=mp[i][j]*(r[j]-l[j]-1);
                    mx=max(mx,sum);
                }
            }
            cout<<mx<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Ansi,UTF8,Unicode,ASCII编码的区别
    Delphi 快捷键
    Sql Server2008恢复备份数据库问题
    js图片无缝滚动代码
    SQL Server 2008 清空删除日志文件 130G日志 10秒内变10M .
    JavaScript_循环26个英文字母的方法 .
    iframe跨子域
    sql数据库该名字
    大型网站架构的优化与架构演变(整理) .转自网络
    删除SQL日志语句,经测试8G日志文件都可以删除
  • 原文地址:https://www.cnblogs.com/ydw--/p/10971440.html
Copyright © 2020-2023  润新知