Second Large Rectangle

题目链接(点击)

题目描述

Given a N×MN \times MN×M binary matrix. Please output the size of second large rectangle containing all "1"\texttt{"1"}"1".

Containing all "1"\texttt{"1"}"1" means that the entries of the rectangle are all "1"\texttt{"1"}"1".

A rectangle can be defined as four integers x1,y1,x2,y2x_1, y_1, x_2, y_2x1​,y1​,x2​,y2​ where 1≤x1≤x2≤N1 \leq x_1 \leq x_2 \leq N1≤x1​≤x2​≤N and 1≤y1≤y2≤M1 \leq y_1 \leq y_2 \leq M1≤y1​≤y2​≤M. Then, the rectangle is composed of all the cell (x, y) where x1≤x≤x2x_1 \leq x \leq x_2x1​≤x≤x2​ and y1≤y≤y2y_1 \leq y \leq y2y1​≤y≤y2. If all of the cell in the rectangle is "1"\texttt{"1"}"1", this is a valid rectangle.

Please find out the size of the second largest rectangle, two rectangles are different if exists a cell belonged to one of them but not belonged to the other.

输入描述:

The first line of input contains two space-separated integers N and M.

Following N lines each contains M characters cijc_{ij}cij​.

1≤N,M≤10001 \leq N, M \leq 10001≤N,M≤1000

N×M≥2N \times M \geq 2N×M≥2

cij∈"01"c_{ij} \in \texttt{"01"}cij​∈"01"

输出描述:

Output one line containing an integer representing the answer. If there are less than 2 rectangles containning all "1"\texttt{"1"}"1", output "0"\texttt{"0"}"0".

输入

1 2
01

输出

0

输入

1 3
101

输出

1

题意:

给出只包含0和1的矩形长和宽,计算出第二大全1矩阵的面积。

思路:

a.先简化成求最大全1矩阵面积:

1、对01矩阵的每一行求列(行)前缀和

2、在每一行中找每个元素的左右边界

例如:

输入矩阵:

1110

1110

0011

0011

第一步:(求列前缀和)

1110

2220

0031

0042

第二步:找左右边界(取第四行 0042 为例)

为便于观察,先画出分布图

对于图中每一个列元素,以其为高的最大矩形边长分别是:

l[1]=0,r[1]=5  S=0*(5-0-1)=0

l[2]=0,r[2]=5  S=0*(5-0-1)=0

l[3]=2,r[3]=4  S=4*(4-2-1)=4

l[1]=2,r[1]=5  S=2*(5-2-1)=4

所以最大面积为4

b.根据左右区间求出了每一行的最大面积后

可以知道 第二大矩形的面积=所有矩形中第二大的面积/最大面积的长或宽-1后相乘

具体实现:

对每一行中的列元素查找左右区间,要利用单调栈 具体讲解:单调栈

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e3;
int n,m;
int high[MAX+5][MAX+5],Stack[MAX+5],l[MAX+5],r[MAX+5];
char a[MAX+5][MAX+5];
int cnt;
int num[5*MAX*MAX+5];
bool flag[MAX+5][MAX+5];
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
high[i][0]=-1,high[i][m+1]=-1;
scanf("%s",a[i]+1);
for(int j=1;j<=m;j++){
if(a[i][j]=='0'){
high[i][j]=0;
continue;
}
if(i==0)
high[i][j]=a[i][j]-'0';
else
high[i][j]=high[i-1][j]+(a[i][j]-'0');
}
}
for(int i=0;i<n;i++){
int top=0;
Stack[top]=0;
for(int j=1;j<=m;j++){
while(high[i][Stack[top]]>=high[i][j]) top--;
l[j]=Stack[top];
Stack[++top]=j;
}
top=0;
Stack[top]=m+1;
for(int j=m;j>=1;j--){
while(high[i][Stack[top]]>=high[i][j]) top--;
r[j]=Stack[top];
Stack[++top]=j;
}
memset(flag,false,sizeof(flag));
for(int j=1;j<=m;j++){
int x=r[j]-l[j]-1,y=high[i][j];
if(!y) continue;
if(!flag[l[j]][r[j]]){ ///标记一行中出现过的左右区间,否则同一个矩形面积会多次计算
num[cnt++]=x*y;
num[cnt++]=(x-1)*y;
num[cnt++]=x*(y-1);
flag[l[j]][r[j]]=true;
}
}
}
sort(num,num+cnt);
printf("%d\n",num[cnt-2]);
return 0;
}
/*
1 7
2145133
0 0 2 3 0 5 5
2 8 5 5 8 8 8
*/

Second Large Rectangle【单调栈】的更多相关文章

  1. 牛客多校第二场H Second Large Rectangle 单调栈or悬线法

    Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...

  2. 2019 牛客暑期多校 第二场 H Second Large Rectangle (单调栈)

    题目:https://ac.nowcoder.com/acm/contest/882/H 题意:一个大的01矩阵,然后现在要求第二大的全一矩阵是多少 思路:在这里我们首先学习一下另一个东西,怎么求直方 ...

  3. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  4. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  5. 2019牛客暑期多校训练营(第二场)-H Second Large Rectangle(次大子矩阵,降维,直方图+单调栈)

    题目链接:https://ac.nowcoder.com/acm/contest/882/H 题目:给n×m的由01组成的矩阵,求次大全1子矩阵的大小. 思路:第一步还是降维操作,用a[i][j]记录 ...

  6. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  7. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  8. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  9. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

随机推荐

  1. 网页导出成word文档的默认视图方式问题

    网页导出成word文档的默认视图方式问题 一般保存后的word文档默认是“Web版式视图”打开,这样会给客户的感觉不是真正的word文档,必须实现打开就是“页面视图” 1. 修改<html> ...

  2. BZOJ 1028 BZOJ 1029 //贪心

    1028: [JSOI2007]麻将 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2197  Solved: 989[Submit][Status][ ...

  3. docker常用命令,及进入Tomcat的WebApps发布目录(就是进入docker容器后台目录)

    docker常用命令,及进入Tomcat的WebApps发布目录(就是进入docker容器后台目录)   一.常用命令 1.显示所有的容器,包括未运行的 docker ps -a   2.启动容器.注 ...

  4. (上)python3 selenium3 从框架实现学习selenium让你事半功倍

    本文感谢以下文档或说明提供的参考. Selenium-Python中文文档 Selenium Documentation Webdriver 参考 如有错误欢迎在评论区指出,作者将即时更改.文章从我的 ...

  5. 100道MySQL数据库经典面试题解析(收藏版)

    前言 100道MySQL数据库经典面试题解析,已经上传github啦 https://github.com/whx123/JavaHome/tree/master/Java面试题集结号 公众号:捡田螺 ...

  6. window端口被占用

    一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务器时就出现了如下图所示的错误: 错误: 代理抛出异常错误: java.rmi.server.ExportExceptio ...

  7. 如何在ARM上运行k3s? 窥探k3s启动过程!,内附容器多平台包构建

    开始之前 最近在对华为云鲲鹏服务器(一种ARM服务器arm64)运行容器可行性做验证,顺便了解了很多ARM和容器相关的知识.一提到arm运行容器首先想到的是k3s,下面是用k3s快速搭建一个kuber ...

  8. [工具-003]如何从ipa中提取info.plist并提取相应信息

    最近公司的产品要进行一次批量的升级,产品中的一些配置存放在info.plist,为了保证产品的信息无误,我们必须要对产品的发布信息进行验证.例如:广告ID,umeng,talkingdata等等.那么 ...

  9. cors跨越深度刨析

    解决跨域的方式JSOP,和CORS JSONP不做介绍了. CORS跨域: 参考阮一峰http://www.ruanyifeng.com/blog/2016/04/cors.html 官方:https ...

  10. 50个SQL语句(MySQL版) 问题八

    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 tea ...