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. 虚拟机ubuntu18.04设置静态IP

    说明: 网关:192.168.8.2 待设置静态IP:192.168.8.25 1.编辑:vi /etc/netplan/01-network-manager-all.yaml 打开以后内容如下: # ...

  2. IIS身份验证知识摘录

    IIS 身份验证 ASP.NET 身份验证分为两个步骤.首先,Internet 信息服务 (IIS) 对用户进行身份验证,并创建一个 Windows 令牌来表示该用户.IIS 通过查看 IIS 元数据 ...

  3. 现代C++学习笔记之二入门篇1

    现代 C++ 强调: 基于堆栈的范围,而非堆或静态全局范围. 自动类型推理,而非显式类型名称. 智能指针而不是原始指针. std::string 和 std::wstring 类型(请参见 <s ...

  4. (十)ASP.NET自定义用户控件(3)

    using HX.DHL.EIP.Services.Def.Localization; using HX.DHL.EIP.Web.Framework; using System; using Syst ...

  5. LibreOJ 6004 圆桌聚餐 (最大流)

    题解:天啊,这道最大流真是水的一批……只需要每张桌子向每个单位建一条容量为1的边,源点向桌子建边,容量为桌子能坐的人数;单位向汇点建边,容量为单位人数即可,然后根据单位与桌子的连边值是否为一来了解每个 ...

  6. POJ - 2109 Power of Cryptography(高精度log+二分)

    Current work in cryptography involves (among other things) large prime numbers and computing powers ...

  7. delphi XE7 在Android编译SharedActivity时出错

    delphi XE6 在Android编译SharedActivity时正常,但xe7下编译出错,在uses添加Androidapi.Helpers就可以.

  8. PLSQL_Developer 连接win7_64位oracle11g

    window7系统 安装的64位 oracle11g,连接32位PLSQL_Developer 1 . 下载 PLSQL_Developer 9.0以上版本(绿色含汉化)   官方的 instantc ...

  9. javascript js函数重名后面的覆盖前面的

    js 函数重名后面的覆盖前面的   var x = 1;    var y = 0;    var z = 0;    function add(n) { return n = n + 1; }    ...

  10. RESTful API概念解析

    什么是restful? REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移”或“表现层状态转化”. ...