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. nyoj84 阶乘的0

    阶乘的0 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示測试数据的组数(1<=N<=1 ...

  2. input 的read only 和 disable的区别

    read only ---------->只能读,不能操作,但是数据可以提交 disable -------------->控件被禁用,数据不能提交

  3. 使用 fcntl 函数 获取,设置文件的状态标志

    前言 当打开一个文件的时候,我们需要指定打开文件的模式( 只读,只写等 ).那么在程序中如何获取,修改这个文件的状态标志呢?本文将告诉你如何用 fcntl函数 获取指定文件的状态标志. 解决思路 1. ...

  4. I NEED A OFFER! hdu1203

    Description Speakless非常早就想出国,如今他已经考完了全部须要的考试,准备了全部要准备的材料,于是.便须要去申请学校了. 要申请国外的不论什么大学,你都要交纳一定的申请费用,这但是 ...

  5. 新版的Spring4X怎样下载

    点击下载 <a href="http://download.csdn.net/detail/zhaoqingkaitt/7733719">点击免费下载</a> ...

  6. OI中字符串读入和处理

    OI中字符串读入和处理 在NOIP的"大模拟"题中,往往要对字符串进行读入并处理,这些字符串有可能包含空格并以\n作为分割,传统的cin >> scanf() 等等,不 ...

  7. linux 网络设备,网卡配置 ,相关

    网络设备,网卡配置: Eth0是物理网卡:唯一mac地址,Bcast:广播地址,MAsk:子网掩码, Lo:系统自带的回环的ip地址,可以做一些基本的测试应用,比如没有网卡就用127.0.0.1, r ...

  8. 【小程序】bindconfirm点击小键盘触发事件、focus自动获取焦点

    最近在写小程序,项目要求写一个搜索框,在进入页面时就触发input的事件,调出键盘,点小键上的搜索按钮 就触发搜索事件,分享一下. bindconfirm 是点击小键盘上的搜索按钮就触发要执行的方法 ...

  9. php排序方法之快速排序

    $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function quickSort($arr){ if (count($arr) <= 1){ retu ...

  10. CodeForces813E:Army Creation (主席树---上一题的加强版)

    As you might remember from our previous rounds, Vova really likes computer games. Now he is playing ...