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 ≤ m, n ≤ 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(思维+单调栈)的更多相关文章
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- [POJ2559&POJ3494] Largest Rectangle in a Histogram&Largest Submatrix of All 1’s 「单调栈」
Largest Rectangle in a Histogram http://poj.org/problem?id=2559 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题 ...
- POJ3493 Largest Submatrix of All 1’s(单调栈)
题目给一个01矩阵,求最大的1子矩阵. 先用dp预处理出每一行的每一列的1能向上按连续的1延伸多少,然后枚举每一行作为子矩阵的底,那样对于每一行的答案就是POJ2559这个经典问题了. #includ ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- Imbalanced Array CodeForces - 817D (思维+单调栈)
You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)
题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...
- 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 ...
随机推荐
- win32 多线程 (五)Event
Event是内核对象,他可以分为自动和手动两种模式. HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManual ...
- 用conda创建一个tensorflow 虚拟环境
创建your——user——name = tensorflow 的虚拟环境 xinpingdeMacBook-Pro:~ xinpingbao$ conda create -n tensorflow ...
- Django框架 之 admin管理工具(组件使用)
Django框架 之 admin管理工具(组件使用) 浏览目录 激活管理工具 使用管理工具 admin的定制 admin组件使用 Django 提供了基于 web 的管理工具. Django 自动管理 ...
- Oracle——控制事务
一.回滚到保留点 使用 SAVEPOINT 语句在当前事务中创建保存点. 使用 ROLLBACK TO SAVEPOINT 语句回滚到创建的保存点. 二.提交或回滚前的数据状态 改变前的数据状态是可以 ...
- 编译boost,去掉不使用的组件
说明:下面内容仅针对Linux环境(boost官网为:http://www.boost.org/,可从这里下载它的源代码包,这里要求下载.tar.gz包,而非.7z..zip或bz2包). 在当前目录 ...
- 人脸识别 人工智能(AI)
.. 如何通过AI实现 用我自己的数据集:能识别几张人脸.能否判断相似度.能否认出.
- MySQL性能调优与架构设计——第5章 备份与恢复
第5章 备份与恢复 前言 数据库的备份与恢复一直都是 DBA 工作中最为重要的部分之一,也是基本工作之一.任何正式环境的数据库都必须有完整的备份计划和恢复测试,本章内容将主要介绍 MySQL数据库的备 ...
- [算法基础]Big O Notation时间复杂度计算方法
首先一点就是无视任何常量 从最简单的开始 statement; 这段时间复杂度为常数1,所以O(1). 然后 ; i < N; i++ ) statement; 这一段是线性的,则时间复杂度为N ...
- 解决jquery操作checkbox火狐下第二次无法勾选问题
最近在学习jQuery(版本jquery-1.9.1.js),要求用jQuery实现全选/全不选.反选,在IE(IE8)中没有问题,但在火狐浏览器中调试的时候出现了一些小问题,达不到效果. html代 ...
- jQuery bind() live()
<script type="text/javascript"> $(document).ready(function () { /*$('.clickme').live ...