poj 3494 Largest Submatrix of All 1’s
来源:互联网 发布:db2端口 编辑:程序博客网 时间:2024/05/07 13:59
Largest Submatrix of All 1’s
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 4903 Accepted: 1828Case Time Limit: 2000MS
Description
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? Bylargest we mean that the submatrix has the most elements.
Input
The input contains multiple test cases. Each test case begins with m andn (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order onm 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 20 00 04 40 0 0 00 1 1 00 1 1 00 0 0 0
Sample Output
04
这道题也是单调栈的应用,一开始不太容易看出来,这里枚举每一行,以该行为底边能够组成的最大1矩阵。这下题目就变成了矩阵最大面积。
具体参考:题解 矩阵最大面积:poj 2559
代码:
#include<cstdio>#include<iostream>#define Maxn 2010using 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][n]=-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;}
0 0
- Largest Submatrix of All 1’s POJ
- Largest Submatrix of All 1’s POJ
- 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
- Largest Submatrix of All 1’s(POJ 3494) 单调栈
- 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
- poj3494 Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s--POJ3494
- Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s
- Largest Submatrix of All 1’s
- 返回顶部的js实现(jQuery/MooTools)
- 最全的常用正则表达式大全
- QListWidget和QListWidgetItem的简单使用
- zoj QS 1586 Network (prim算法)
- Map/Reduce原理深入浅出
- poj 3494 Largest Submatrix of All 1’s
- OC-property-synthesize-nonatomic-atomic-assign-retain-copy
- Cocos2d-x 瓦块地图小游戏 (六) 分数显示 音乐预载 看瓜人
- RPM包的制作
- 黑马程序员-C语言学习日记(五)预编译、类型定义及static
- 如何基于Cocos2d-x v3.x实现A星寻路算法
- 职场分享:职场精英所需要具备的能力
- jquery动态生成表单提交
- 使用node.js半年来总结的经验