poj 3494 Largest Submatrix of All 1’s
2014-08-20 15:17
435 查看
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
Sample Output
这道题也是单调栈的应用,一开始不太容易看出来,这里枚举每一行,以该行为底边能够组成的最大1矩阵。这下题目就变成了矩阵最大面积。
具体参考:题解 矩阵最大面积:poj
2559
代码:
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 4903 | Accepted: 1828 | |
Case Time Limit: 2000MS |
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
这道题也是单调栈的应用,一开始不太容易看出来,这里枚举每一行,以该行为底边能够组成的最大1矩阵。这下题目就变成了矩阵最大面积。
具体参考:题解 矩阵最大面积:poj
2559
代码:
#include<cstdio> #include<iostream> #define Maxn 2010 using namespace std; int st[Maxn],idx[Maxn],a[Maxn][Maxn]; int dstack(int x,int n){ int top=-1,maxx=0,ans; for(int i=0;i<=n;i++){ int id=i; while(top!=-1&&st[top]>a[x][i]){ id=idx[top]; ans=(i-id)*st[top--]; maxx=max(maxx,ans); } st[++top]=a[x][i]; idx[top]=id; } return maxx; } int main() { int n,m; while(~scanf("%d%d",&n,&m)){ for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%d",&a[i][j]); for(int i=0;i<n;i++) a[i] =-1; for(int i=n-2;i>=0;i--) for(int j=0;j<n;j++) if(a[i][j]) a[i][j]+=a[i+1][j]; int res=0; for(int i=0;i<n;i++) res=max(res,dstack(i,n)); printf("%d\n",res); } return 0; }
相关文章推荐
- POJ-3494-Largest Submatrix of All 1’s
- POJ-3494-Largest Submatrix of All 1’s-还是单调栈
- POJ 3494 Largest Submatrix of All 1's(最大全1子矩阵面积、单调栈)
- POJ 3494 Largest Submatrix of All 1’s
- POJ 3494 Largest Submatrix of All 1’s 笔记
- poj3494 Largest Submatrix of All 1’s 单调栈+dp
- POJ 3494 Largest Submatrix of All 1’s
- (POJ 3494)Largest Submatrix of All 1’s (DP、单调栈)
- POJ - 3494 Largest Submatrix of All 1’s(单调栈)
- POJ 3494 Largest Submatrix of All 1’s(最大子图形)
- POJ 3494 Largest Submatrix of All 1's 栈的运用 好题
- POJ 3494__Largest Submatrix of All 1’s
- 【POJ】3494 Largest Submatrix of All 1’s
- poj 3494 Largest Submatrix of All 1’s(单调栈)
- POJ 3494 Largest Submatrix of All 1’s 单调栈(数组版)
- poj 3494 Largest Submatrix of All 1’s
- POJ 3494 Largest Submatrix of All 1’s(单调栈||dp)
- poj 3494 Largest Submatrix of All 1’s
- POJ 3494 Largest Submatrix of All 1’s 单调栈应用 图解+代码详解
- Largest Submatrix of All 1’s(POJ 3494) 单调栈