POJ 3494 Largest Submatrix of All 1’s(单调栈)

题目:

Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 5540   Accepted: 2085
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 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

解题思路:

  这道题是说,一个矩阵是仅仅由0-1组成的矩阵,求这个矩阵的的含有全部1的最大子矩阵。

  那么一开始拿到这个题后只会暴力,就是每次枚举一个点,然后从这个点开始看横向跑到多远,纵向跑到多远,然后求出最大的值出来。

  但是,结合刚刚写过的一道裸的单调栈的题目,我们就可以知道,只要我们每次对这一行建立一次单调栈就行了。O(n)的时间复杂度。

代码:

 1 # include <cstdio>
 2 # include <iostream>
 3 # include <cstring>
 4 # include <stack>
 5 using namespace std;
 6 # define MAX 2333
 7 int a[MAX][MAX];
 8 int u[MAX][MAX],l[MAX][MAX],r[MAX][MAX];
 9 stack<pair<int,int> >S;
10 int n,m;
11 void pre()
12 {
13     memset(u,0,sizeof(u));
14     for ( int i = 1;i <= n;i++ )
15     {
16         for ( int j = 1;j <= m;j++ )
17         {
18             if ( a[i][j]==0 ) 
19                 u[i][j] = 0;
20             else
21                 u[i][j] = u[i-1][j]+1;
22         }
23     }
24 }
25 int main(void)
26 {
27     while ( scanf("%d%d",&n,&m)!=EOF )
28     {
29         for ( int i = 1;i <= n;i++ )
30         {
31             for ( int j = 1;j <= m;j++ )
32             {
33                 scanf("%d",&a[i][j]);
34             }
35         }
36         pre();
37         for ( int i = 1;i <= n;i++ )
38         {
39             while ( !S.empty() ) S.pop();
40             for ( int j = 1;j <= m;j++ )
41             {
42                 while ( !S.empty()&&u[i][j]<=S.top().first ) S.pop();
43                 if ( S.size()==0 ) 
44                     l[i][j] = 1;
45                 else
46                     l[i][j] = S.top().second+1;
47                 S.push(pair<int,int>(u[i][j],j));
48             }
49         }
50         for(int i = 1;i <= n;i++)
51         {  
52             while(!S.empty()) S.pop();  
53             for(int j = m;j >= 1;j--)
54             {  
55                 while(!S.empty() && u[i][j] <= S.top().first) S.pop();  
56                 if(S.size() == 0) 
57                     r[i][j] = m;  
58                 else 
59                     r[i][j] = S.top().second-1;  
60                 S.push(pair<int,int>(u[i][j],j));  
61             }  
62         }  
63         int ans = 0;
64         for ( int i = 1;i <= n;i++ )
65         {
66             for ( int j = 1;j <= m;j++ )
67             {
68                 ans = max(ans,u[i][j]*(r[i][j]-l[i][j]+1));
69             }
70         }
71         printf("%d\n",ans);
72     }
73     return 0;
74 }

 

posted @ 2016-04-15 23:58  西瓜书?蓝皮书?  阅读(221)  评论(0编辑  收藏  举报