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

    Largest Submatrix of All 1’s
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 8551   Accepted: 3089
    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 ≤ mn ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with nnumbers. 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

    题意:

    求内部全部都是1的子矩阵最大面积。

    思路:

    第一时间想到了一个n的3次方的dp,但是这样肯定超时。

    类似的题 :https://www.cnblogs.com/ZGQblogs/p/10664506.html

    如果不是在学单调栈,我觉得我一定是想不到的。

    首先就是维护每一行的每一个位置,如果以当前行为底,上面连续的1有多少个。

    然后就是这个题:http://poj.org/problem?id=2559

    代码:

    加了读入挂才过~~~

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<vector>
      4 #include<stack>
      5 #include<queue>
      6 #include<map>
      7 #include<set>
      8 #include<cstdio>
      9 #include<cstring>
     10 #include<cmath>
     11 #include<ctime>
     12 #define fuck(x) cout<<#x<<" = "<<x<<endl;
     13 #define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
     14 #define ls (t<<1)
     15 #define rs ((t<<1)+1)
     16 using namespace std;
     17 typedef long long ll;
     18 typedef unsigned long long ull;
     19 const int maxn = 2086;
     20 const int maxm = 2086;
     21 const int inf = 2.1e9;
     22 const ll Inf = 999999999999999999;
     23 const int mod = 1000000007;
     24 const double eps = 1e-6;
     25 const double pi = acos(-1);
     26 
     27 int l[maxn],r[maxn];
     28 struct node{
     29     int num,pos;
     30 };
     31 int mp[maxn][maxn];
     32 stack<node>st;
     33 
     34 
     35 int solve(int *num,int n){
     36     for(int i=1;i<=n;i++){
     37         l[i]=r[i]=i;
     38     }
     39     num[0]=num[n+1]=-1;
     40     for(int i=1;i<=n+1;i++){
     41         while(!st.empty()&&st.top().num>num[i]){
     42             r[st.top().pos]=i-1;
     43             st.pop();
     44         }
     45         st.push(node{num[i],i});
     46     }
     47     while(!st.empty()){st.pop();}
     48 
     49     for(int i=n;i>=0;i--){
     50         while(!st.empty()&&st.top().num>num[i]){
     51             l[st.top().pos]=i+1;
     52             st.pop();
     53         }
     54         st.push(node{num[i],i});
     55     }
     56     ll ans=0;
     57     for(int i=1;i<=n;i++){
     58         ans=max(ans,1ll*num[i]*(r[i]-l[i]+1));
     59     }
     60     while(!st.empty()){st.pop();}
     61     return ans;
     62 }
     63 
     64 char buf[maxn], *ps = buf, *pe = buf+1;
     65 inline void rnext(){
     66     if(++ps == pe)
     67         pe = (ps = buf)+fread(buf,1,sizeof(buf),stdin);
     68 }
     69 template <class T>
     70 inline bool in(T &ans)
     71 {
     72     ans = 0;
     73     T f = 1;
     74     if(ps == pe) return false;
     75     do{
     76         rnext();
     77         if('-' == *ps) f = -1;
     78     }while(!isdigit(*ps) && ps != pe);
     79     if(ps == pe) return false;
     80     do
     81     {
     82         ans = (ans<<1)+(ans<<3)+*ps-48;
     83         rnext();
     84     }while(isdigit(*ps) && ps != pe);
     85     ans *= f;
     86     return true;
     87 }
     88 
     89 int main()
     90 {
     91 //    freopen("in.txt","r",stdin);
     92     int n,m;
     93     while(true){
     94         in(n);in(m);
     95         if(!m||!n){break;}
     96         int ans=0;
     97         for(int i=1;i<=n;i++){
     98             for(int j=1;j<=m;j++){
     99                 in(mp[i][j]);
    100 //                cout<<mp[i][j]<<endl;
    101                 if(mp[i][j]==1){mp[i][j]+=mp[i-1][j];}
    102             }
    103             ans=max(ans,solve(mp[i],m));
    104         }
    105         printf("%d
    ",ans);
    106     }
    107     return 0;
    108 }
    View Code
  • 相关阅读:
    SPI简述
    stm32和sd卡
    BKP和RTC
    Lwip与底层的接口
    关于Ad-hoc
    stm32 引脚映射 和 ADC
    GDB使用总结
    linux管道和重定向
    学习python的第四天
    学习pyton的第三天
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10789346.html
Copyright © 2011-2022 走看看