POJ 3494 Largest Submatrix of All 1's 栈的运用 好题


Language:
Default

Largest Submatrix of All 1’s

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 5185   Accepted: 1950
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

POJ Founder Monthly Contest – 2008.01.31, xfxyjwf

题意:给出一个n*m的矩阵,这个矩阵的元素为0或1,要求找出一个子矩阵,满足:

1.都是由1组成

2.矩阵的面积最大

这道题,其实就是POJ2559的加强版

a[i][j]表示:第i行,以第j列为底的矩阵的最大高度。

之后,枚举每一列,相当于以每一列为底的条形图,求最大的矩形面积(就是POJ2559了)

注意:我这份代码交了3次,2次ac,1次tle,因为有些单样例tle了,下次再修改一下。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<stack>
 5 using namespace std;
 6
 7 const int maxn=2005;
 8
 9 int a[maxn][maxn];
10 int L[maxn];
11 int R[maxn];
12 struct Node
13 {
14     int num,h;
15 }node[maxn];
16
17 int solve(int n,int cnt)
18 {
19     for(int i=1;i<=n;i++)
20     {
21         node[i].h=a[i][cnt];
22         node[i].num=i;
23     }
24     node[0].num=0;
25     node[0].h=0;
26     n++;
27     node[n].num=n;
28     node[n].h=0;
29     stack<Node>s;
30     s.push(node[0]);
31     for(int i=1;i<=n;i++)
32     {
33         if(node[i].h>=s.top().h)
34             s.push(node[i]);
35         else
36         {
37             while(s.top().h>node[i].h)
38             {
39                 Node tmp=s.top();
40                 s.pop();
41                 R[tmp.num]=i;
42                 L[tmp.num]=s.top().num;
43             }
44             s.push(node[i]);
45         }
46     }
47     int ans=-1;
48     for(int i=1;i<n;i++)
49     {
50         ans=max(ans,node[i].h*(R[i]-L[i]-1));
51     }
52     return ans;
53 }
54
55 int main()
56 {
57     int row,col;
58     while(~scanf("%d%d",&row,&col))
59     {
60         for(int i=1;i<=row;i++)
61         {
62             a[i][0]=0;
63             for(int j=1;j<=col;j++)
64             {
65                 scanf("%d",&a[i][j]);
66                 if(a[i][j-1]&&a[i][j])
67                 {
68                     a[i][j]+=a[i][j-1];
69                 }
70             }
71         }
72         int ans=-1;
73         for(int j=1;j<=col;j++)
74         {
75             ans=max(ans,solve(row,j));
76         }
77         printf("%d\n",ans);
78
79     }
80     return 0;
81 }

POJ 3494 Largest Submatrix of All 1's 栈的运用 好题

时间: 2024-08-09 22:39:50

POJ 3494 Largest Submatrix of All 1's 栈的运用 好题的相关文章

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 w

POJ 3494 Largest Submatrix of All 1’s

POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn=2000+10; int a[maxn]; int L[maxn],R[maxn]; int m,n;

POJ 2559 Largest Rectangle in a Histogram(单调栈)

Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectang

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

Largest Submatrix(动态规划)

Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2018    Accepted Submission(s): 967 Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you ca

hdu 2870 Largest Submatrix

Largest Submatrix http://acm.hdu.edu.cn/showproblem.php?pid=2870 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change '

poj 2559 Largest Rectangle in a Histogram 栈

// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的是数据结构做,也可以递推做,目前只会数据结构的 // // 对于每个高度h,求一个左边界L和右边界R,分别表示的意义是 // L是下标为j的矩形的高度的hj小于当前h的最大的j的值.则根据定义 // 我们可以知道j到i之间的h都是大于当前的hi的. // R是下标为k的矩形的高度的hk大于当前h的最

HDU 2870 Largest Submatrix (单调栈)

http://acm.hdu.edu.cn/showproblem.php?pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1569    Accepted Submission(s): 748 Problem Description Now here is a matrix wit

POJ 2559 Largest Rectangle in a Histogram RMQ || 单调栈

题目链接:点击打开链接 题意就是求最大面积 枚举每个柱子作为起点 然后二分两边长度. 求个区间最值. #include<stdio.h> #include<iostream> #include<math.h> using namespace std; #define ll long long #define N 100100 inline bool rd(int &n){ int x = 0, tmp = 1; char c = getchar(); while