太子丶
愿你走出半生,归来仍为少年。

POJ 3494 Largest Submatrix of All 1’s

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


释意:
  给出一个由0,1组合的矩阵,试求出元素最多且都是1的矩形。 poj2559加强版。
题解:
1.将矩阵看成不同高度矩形,若相邻两行中若有1相邻则次矩形的高度便加1,将每一行分别看做矩形的底进行更新,从而问题变成了找面积最大矩形
举例说明:
因为我们要找的是矩形,所以它一定是以 某个行元素开始的,如果枚举行的时候,我们会发现:
对于第一行: 


对于第二行:


第三行:


第四行: 


这样的话,其实我们要找到的某个矩形就转换成 一某一个行开始的 histogram的最大矩形问题了。

那么我们原始矩形可以变成如下的形式的数据:


第一行表示,我们以第一行作为底边,所形成的矩形的高度,其他行也类似。

2.求以第i行为底的最大矩形,利用单调队列或者单调栈,以单调队列为例:

对该行中的每个点的高度为最小基准向左右两边进行松弛,求其可满足的左右区间的最大区间。

以右区间为例:

从该行的右端点开始一次进队遍历,建立单调增的队列将队尾与当前点的高度进行比较,若大于等于,则队尾出队,否则队尾元素的下标便是以当前点高度为最小高度的矩形的右区间最大值+1.

左区间同理亦可得到,从而确定该点所决定的矩形的大小,进而却定以该行为底的最大矩形。

3.代码实现:

 1 //单调队列实现  C++提交 若用G++则会T!!!!! 
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <map>
 9 #include<string.h>
10 #include<stack>
11 #include<set>
12 #include <queue>
13 using namespace std;
14 struct Node
15 {
16     int val;
17     int position;
18 }q[2020];              // 数组模拟单调递增队列
19 int n,m;
20 int l[2020];           // 以当前点高度为最小高度的矩形的左区间最大值
21 int r[2020];           //以当前点高度为最小高度的矩形的右区间最大值
22 int a[2020][2020];
23 //求每一行的最大矩形
24 int AREA(int nn)
25 {
26     int h = 1; //队头
27     int tail = 1;//队尾
28     //确定以每一个点的高度为最小值的矩形的右区间的最大值
29     q[1].position = n+1;
30     q[1].val = -1;
31     for(int i = m;i>=1;i--)
32     {
33         while(q[tail].val >= a[nn][i]) tail--;
34         r[i] = q[tail].position-i;
35         q[++tail].val = a[nn][i];
36         q[tail].position = i;
37     }
38     //确定以每一个点的高度为最小值的矩形的左区间的最大值
39     h = 1;
40     tail = 1;
41     q[1].position = 0;
42     q[1].val = -1;
43     for(int i = 1;i<=m;i++)
44     {
45          while(q[tail].val>=a[nn][i]) tail--;
46          l[i] = i-q[tail].position;
47          q[++tail].val = a[nn][i];
48          q[tail].position = i;
49     }
50     //该行中所有矩形的最大值
51     int max1 = -1;
52     for(int i = 1;i<=m;i++)
53         max1 = max(max1,(l[i]+r[i]-1)*a[nn][i]);
54     return max1;
55 }
56 int main()
57 {
58 
59     while(~scanf("%d %d",&n,&m))
60     {
61         for(int i = 1;i<=n;i++)
62             for(int j = 1;j<=m;j++)
63                 scanf("%d",&a[i][j]);
64         //讲矩阵看做不同高度的矩形进行更新操作
65         for(int i = 2;i<=n;i++)
66             for(int j = 1;j<=m;j++)
67                 if(a[i][j]) a[i][j] = a[i-1][j]+a[i][j];
68         int max1 = -1;
69         //对每行进行遍历确定每行的最大矩形
70         for(int i = 1;i<=n;i++)
71             max1 = max(AREA(i),max1);
72         printf("%d\n",max1);
73     }
74     return 0;
75 }
 1 //单调栈实现
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <map>
 9 #include<string.h>
10 #include<stack>
11 #include<set>
12 #include <queue>
13 
14 using namespace std;
15 struct re
16 {
17     int h;
18     int p;
19 };
20 
21 int m,n;
22 int a[2002][2002];
23 int h[2002][2002];
24 int l[2002];
25 int r[2002];
26 int AREA(int R)
27 {
28     int i,j;
29     int max1 = -1;
30     stack<re> s;
31     //求左区间
32     re p0,pi;
33     p0.h = -1;
34     p0.p = -1;
35     s.push(p0);
36     for(i = 0;i<n;i++)
37     {
38         pi.p = i;
39         pi.h = h[R][i];
40         while(s.top().h>=pi.h) s.pop();
41         l[i] = i-s.top().p;
42         s.push(pi);
43     }
44     while(!s.empty()) s.pop();
45     //求右区间
46      p0.h = -1;
47      p0.p = n;
48      s.push(p0);
49     for(i =n-1 ;i>=0;i--)
50     {
51         pi.p = i;
52         pi.h = h[R][i];
53         while(s.top().h>=pi.h) s.pop();
54         r[i] = s.top().p-i;
55         s.push(pi);
56     }
57     for(i = 0;i<n-1;i++)
58         if((r[i]+l[i]-1)*h[R][i]>max1)
59             max1= (r[i]+l[i]-1)*h[R][i];
60     return max1;
61 }
62 int main()
63 {
64     int i,j;
65     while(~scanf("%d%d",&m,&n))
66     {
67         for(i = 0; i<m; i++)
68             for(j = 0; j<n; j++)
69             {
70                 scanf("%d",&a[i][j]);
71                 h[i][j] = a[i][j];
72             }
73         for(i = 0; i<n; i++)
74             for(j = 1; j<m; j++)
75                 if(h[j][i]) h[j][i] += h[j-1][i];
76         int max1 = -1;
77         for(i = 0; i<m; i++) max1 = max(max1,AREA(i));
78         printf("%d\n",max1);
79     }
80 }

 

 

 

 

本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
posted on 2017-08-04 15:52  太子丶  阅读(210)  评论(0编辑  收藏  举报