Largest Submatrix of All 1’s

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 mlines 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 很巧妙的方法。把图拆成一行行来做,分别求以每行为底的最大矩形面积,类似lc课后辅导。最后比较出最大值即可。
#include<stdio.h>
#include<stack>
using namespace std; int a[][];
int l[],r[]; int main()
{
int n,m,max,i,j;
while(~scanf("%d%d",&n,&m)){
max=;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
scanf("%d",&a[i][j]);
if(a[i][j]==) a[i][j]+=a[i-][j];
}
}
for(i=;i<=n;i++){
stack<int> s;
for(j=;j<=m;j++){
while(s.size()&&a[i][s.top()]>=a[i][j]) s.pop();
l[j]=s.size()==?:s.top()+;
s.push(j);
}
while(s.size()){
s.pop();
}
for(j=m;j>=;j--){
while(s.size()&&a[i][s.top()]>=a[i][j]) s.pop();
r[j]=s.size()==?m:s.top()-;
s.push(j);
}
for(j=;j<=m;j++){
if(a[i][j]*(r[j]-l[j]+)>max) max=a[i][j]*(r[j]-l[j]+);
}
}
printf("%d\n",max);
}
return ;
}

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 Description Given a m-by-n (0,1)-matrix, of all its submatrice ...

  2. POJ 3494 Largest Submatrix of All 1’s(最大全1子矩阵)

    题目链接:http://poj.org/problem?id=3494 题意:给出一个01的矩阵,找出一个面积最大的全1矩阵. 思路:用h[i][j]表示从位置(i,j)向上连续1的最大长度.之后枚举 ...

  3. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

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

    [题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...

  5. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

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

    嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...

  7. POJ 3494 Largest Submatrix of All 1’s

    POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #incl ...

  8. POJ 3494 Largest Submatrix of All 1’s(最大子图形)

    [题目链接] http://poj.org/problem?id=3494 [题目大意] 在01矩阵中求最大全1子矩形 [题解] 在处理每个点的时候,继承上一个点等高度下的左右最大扩展, 计算在该层的 ...

  9. POJ3494Largest Submatrix of All 1’s[单调栈]

    Largest Submatrix of All 1’s Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 5883   Ac ...

随机推荐

  1. lua 字符串处理

    匹配模式(pattern) . 任何单个字符 %a 任何字母 %c 任何控制字符 %d 任何数字 %g 任何除空白符外的可打印字符 %l 所有小写字母 %p 所有标点符号 %s 所有空白字符 %u 所 ...

  2. Data Matrix Font and Encoder条码控件可以以字体的形式来打印DataMatrix条形码

    Data Matrix Font and Encoder条码控件使您能够以字体的形式来打印DataMatrix条形码. 本产品能够在不论什么支持Java类库..NET动态链接库或Windows COM ...

  3. Spring AOP和IOC(转载)

    spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实 ...

  4. 程序基石系列之C++多态的前提条件

    准备知识 C++中多态(polymorphism)有下面三个前提条件: 必须存在一个继承体系结构. 继承体系结构中的一些类必须具有同名的virtual成员函数(virtualkeyword) 至少有一 ...

  5. MVC入门——列表页

    创建控制器UserInfoController using System; using System.Collections.Generic; using System.Linq; using Sys ...

  6. iOS开发-14款状态栏(StatusBar)开源软件

    本文转载至 http://mobile.51cto.com/hot-418125.htm 之前逛街看到移动做推广,有一个定位应用挺好的,合理的利用了状态栏,做了一些消息提醒和隐藏动画,自己回家就做了一 ...

  7. 【BZOJ3782】上学路线 组合数+容斥+CRT

    [BZOJ3782]上学路线 Description 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不 ...

  8. [Phoenix] 八、动态列

    摘要: 传统关系型数据库的动态列实现只能依赖逻辑层的设计实现,而Phoenix是HBase上的SQL层,借助HBase特性实现的动态列功能,具有高度的灵活性,告别业务逻辑层的复杂设计. 一.概要 动态 ...

  9. 2018-11-9-匿名函数&递归函数初识

    1.匿名函数(lambda) 2.递归函数初识

  10. Maximum likelihood from incomplete data via the EM algorithm (1977)

    Maximum likelihood from incomplete data via the EM algorithm (1977)