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

题意:

找最大的为1的子矩阵,

一开始的错误代码:H是表示他的连续高度,L和R是左边第一个比他小的坐标和右边比他小的坐标,这个过程都是单调栈维护的,

然后暴力枚举最大的点,但是这样有错误,就是L到R这个区间内H的值不一定是相同的

错误代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath> const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int n,m;
int Map[2005][2005];
int H[2005][2005];
int L[2005][2005];
int R[2005][2005];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
while(cin>>n>>m)
{
for(int t=1;t<=n;t++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&Map[t][j]);
}
}
for(int t=1;t<=n;t++)
{
for(int j=1;j<=m;j++)
{
if(Map[t][j]==1)
{
H[t][j]=1;
}
else
{
H[t][j]=0;
}
}
}
memset(L,0,sizeof(L));
for(int t=1;t<=n;t++)
{
for(int j=1;j<=m;j++)
{
R[t][j]=m+1;
}
}
for(int t=2;t<=n;t++)
{
for(int j=1;j<=m;j++)
{
if(H[t-1][j]!=0&&H[t][j]==1)
{
H[t][j]=H[t-1][j]+1;
}
}
}
for(int t=1;t<=n;t++)
{
stack<int>S1;
for(int j=1;j<=m;j++)
{
while(S1.size() && Map[t][S1.top()]>=Map[t][j]) S1.pop();
if(S1.empty()) L[t][j]= 0;
else L[t][j] = S1.top();
S1.push(j);
}
}
for(int t=1;t<=n;t++)
{
stack<int>S1;
for(int j=m;j>=1;j--)
{
while(S1.size() && Map[t][S1.top()]>=Map[t][j]) S1.pop();
if(S1.empty()) R[t][j]= m+1;
else R[t][j] = S1.top();
S1.push(j);
}
}
int manxx=0; for(int t=1;t<=n;t++)
{ for(int j=1;j<=m;j++)
{
if(H[t][R[t][j]-1]==H[t][L[t][j]+1])
manxx=max(manxx,H[t][j]*(R[t][j]-L[t][j]-1));
}
}
cout<<manxx<<endl;
}
return 0;
}

AC代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<cmath>
const int maxn=2e5+5;
typedef long long ll;
using namespace std;
int H[2005];
int a[2005];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,m,x,top,tmp,maxnxx;
stack<int>S1;
while(cin>>n>>m)
{
maxnxx=0;
memset(H,0,sizeof(H));
for(int t=0;t<n;t++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
if(x==1) H[j]=H[j]+1;
else H[j]=0;
a[j]=H[j];
}
a[m+1]=-1;
for(int j=1;j<=m+1;j++)
{
if(S1.empty()||a[j]>=a[S1.top()])
{
S1.push(j);
}
else
{
while(!S1.empty()&&a[j]<a[S1.top()])
{
top=S1.top();
S1.pop();
tmp=(j-top)*a[top];
if(tmp>maxnxx) maxnxx=tmp;
}
S1.push(top);
a[top]=a[j];
}
}
}
cout<<maxnxx<<endl;
}
return 0;
}

Largest Submatrix of All 1’s(思维+单调栈)的更多相关文章

  1. POJ-3494 Largest Submatrix of All 1’s (单调栈)

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

  2. [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」

    Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...

  3. POJ3493 Largest Submatrix of All 1’s(单调栈)

    题目给一个01矩阵,求最大的1子矩阵. 先用dp预处理出每一行的每一列的1能向上按连续的1延伸多少,然后枚举每一行作为子矩阵的底,那样对于每一行的答案就是POJ2559这个经典问题了. #includ ...

  4. [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  5. Imbalanced Array CodeForces - 817D (思维+单调栈)

    You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...

  6. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

  7. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

  8. Stack Sorting CodeForces - 911E (思维+单调栈思想)

    Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...

  9. 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 ...

随机推荐

  1. android样式相关

    转载自Keegan小钢,原文链接http://keeganlee.me/post/android/20150830

  2. 配置springboot在访问404时自定义返回结果以及统一异常处理

    在搭建项目框架的时候用的是springboot,想统一处理异常,但是发现404的错误总是捕捉不到,总是返回的是springBoot自带的错误结果信息. 如下是springBoot自带的错误结果信息: ...

  3. linux 软链接 硬链接

    查看文件sun.txt   加上参数i 是显示节点 inode [root@bogon test]# ls -li sun.txt 10006225 -rw-r--r--. 1 root root 0 ...

  4. hibernate 框架的简单使用

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuratio ...

  5. CKEdiotr入门级使用

    CKEdiotr是一款不错的网页富文本编辑器,其内置的功能最大满足用户的需求,先将CKEditor的简单实用做个总结,以便于日后查看.使用.我用的是.net平台,故而以下介绍的是.net的CKEdit ...

  6. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(2):SSM+Redis概念理解

    一.SSM+Redis的结构图 在Java互联网中,以Spring+SpringMVC+MyBatis(SSM)作为主流框架,SSM+Redis的结构图如下: 二.下面介绍它们各自承担的功能: 1.S ...

  7. py_initialize:C调Python出错 是初始化错误?

    还是pythonpath和pythonname变量没有配置正确? py_initialize()方法是什么? In an application embedding Python, this shou ...

  8. 编写高质量代码改善C#程序的157个建议——建议28:理解延迟求值和主动求值之间的区别

    建议28:理解延迟求值和主动求值之间的区别 要理解延迟求值(lazy evaluation)和主动求值(eager evaluation),先看个例子: List<, , , , , , , , ...

  9. go的同步模型

    首先来看一段代码,这是The Go Memory Model一文中的一个例子   var a, b int   func f() {     a = 1     b = 2 } func g() { ...

  10. WM_QUERYENDSESSION与WM_ENDSESSION

    此文已由作者王荣涛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 首先XP系统和Vista以后的系统,这两个消息的处理方式是不同的. XP系统 系统发送WM_QUERYEND ...