zoukankan      html  css  js  c++  java
  • Largest Submatrix of All 1’s(单调栈)

    Largest Submatrix of All 1’s
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 9286   Accepted: 3336
    Case Time Limit: 2000MS

    Description

    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

    Source

     
    解题思路: 枚举每一行 以改行为底矩形的最大值 转换为了这个问题(https://www.cnblogs.com/qq-1585047819/p/11347741.html); 维护两个单调队列
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <map>
     6 using namespace std;
     7 
     8 int n,m,maxx;
     9 const int N=2005;
    10 int dp[N];
    11 int sta[N],top;
    12 int ma1[N],ma2[N];
    13 
    14 inline int read(){
    15     int x=0,f=1;
    16     char ch=getchar();
    17     while(ch<'0'||ch>'9'){
    18         if(ch=='-')
    19             f=-1;
    20         ch=getchar();
    21     }
    22     while(ch>='0'&&ch<='9'){
    23         x=(x<<1)+(x<<3)+(ch^48);
    24         ch=getchar();
    25     }
    26     return x*f;
    27 }
    28 
    29 int main(){
    30     while(cin>>n>>m){
    31         memset(dp,0,sizeof(dp));
    32         memset(sta,0,sizeof(sta));
    33         top=0;maxx=0;
    34         for(int i=1;i<=n;i++){
    35             for(int j=1,d;j<=m;j++){
    36                 d=read();
    37                 dp[j]=d==0?0:++dp[j];
    38             }
    39             memset(sta,0,sizeof(sta));
    40             top=0;
    41             for(int j=1;j<=m;j++){
    42                 int flag=0;
    43                 while(dp[sta[top]]>=dp[j]&&top) top--,flag=1;
    44                 if(flag==0) ma1[j]=j-1;
    45                 else ma1[j]=top==0?0:sta[top];
    46                 sta[++top]=j;
    47             }
    48             memset(sta,0,sizeof(sta));
    49             top=0;
    50             for(int j=m;j>=1;j--){
    51                 int flag=0;
    52                 while(dp[sta[top]]>=dp[j]&&top) top--,flag=1;
    53                 if(flag==0) ma2[j]=j+1;
    54                 else ma2[j]=top==0?m+1:sta[top];
    55                 sta[++top]=j;
    56             }
    57             for(int j=1;j<=m;j++){
    58                 maxx=max(maxx,(ma2[j]-ma1[j]-1)*dp[j]);
    59             }
    60         }
    61         cout << maxx << endl;
    62     }
    63 }
    View Code
  • 相关阅读:
    第03组 Alpha冲刺(2/4)
    第03组 Alpha冲刺
    第09组 Beta版本演示
    第09组 Beta冲刺(4/4)
    第09组 Beta冲刺(3/4)
    第09组 Beta冲刺(2/4)
    第09组 Beta冲刺(1/4)
    第09组 Alpha事后诸葛亮
    第09组 Alpha冲刺(4/4)
    第09组 Alpha冲刺(3/4)
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11348760.html
Copyright © 2011-2022 走看看