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. cocos+kbe问题记录

    1.不要使用setTimeout函数 setTimeout函数,setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 是HTML DOM中的方法,在游戏中使用,属于全局的延时,当游 ...

  2. 运单waybill快速录入

    运单waybill快速录入 前台页面: 1修改页面onAfterEdit事件, 后台代码:ajax响应回请求1 为成功,0 为失败

  3. 面试题:各大公司Java后端开发面试题总结 已看1 背1 有用 链接有必要看看

    ThreadLocal(线程变量副本)       --整理 Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量. 采用空间换时间,它用于线程间的数据隔离,为每一个 ...

  4. Luogu 4721 【模板】分治 FFT

    还不会这题的多项式求逆的算法. 发现每一项都是一个卷积的形式,那么我们可以使用$NTT$来加速,直接做是$O(n^2logn)$的,我们考虑如何加速转移. 可以采用$cdq$分治的思想,对于区间$[l ...

  5. CodeForces 489E Hiking (二分+DP)

    题意: 一个人在起点0,有n个休息点,每个点有两个数值,分别表示距离起点的距离xi,以及所获得的愉悦值bi,这个人打算每天走L距离,但实际情况不允许他这么做.定义总体失望值val = sum(sqrt ...

  6. sql多表链接之三表连接查询

    表与表之间的关系如下 查询条件:根据员工表的enployee_id 查找他在哪个部门,他在哪个城市工作. 查询语句:

  7. create-react-app设置proxy反向代理不起作用

    在CRA2.X升级以后对proxy的设置做了修改,引用官方升级文档: Object proxy configuration is superseded by src/setupProxy.js To ...

  8. 独立部署GeoWebCache

    在进行GIS项目开发中,常使用Geoserver作为开源的地图服务器,Geoserver是一个JavaEE项目,常通过Tomcat进行部署.而GeoWebCache是一个采用Java实现用于缓存WMS ...

  9. SQL Server乱码处理(ASCII)

    CREATE FUNCTION [dbo].[RegexReplace] ( @string VARCHAR(MAX), --被替换的字符串 @pattern VARCHAR(255), --替换模板 ...

  10. Hexo基本使用

    1. 简述 Hexo是一个基于Node.js的博客系统,常用于搭建在GitHub上. 其本质其实是一个基于Node.js的内嵌webserver的程序,可以通过一些命令根据markdown生成静态页面 ...