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. 强连通 反向建图 hdu3639

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. bzoj1497最大闭权图基础题

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 5485  Solved: 2661[Submit][Status] ...

  3. CF55D

    题目大意: 定义:beautiful number,一种能整除它的所有非 0 数位的数字. 给你 l 和 r,请求出 [l,r] 中 beautiful number 的个数. 解题思路: 数位 DP ...

  4. SSE系列内置函数中的shuffle函数

    SSE 系列内置函数中的 shuffle 函数 邮箱: quarrying@qq.com 博客: http://www.cnblogs.com/quarryman/ 发布时间: 2017年04月18日 ...

  5. JZOJ 4611. 【NOI2016模拟7.11】接水问题 (贪心+A*+可持久化线段树)

    Description: https://gmoj.net/senior/#main/show/4611 题解: 先把A从大到小排序,最小的由排序不等式显然. 考虑类似第k短路的A*的做法. 定义状态 ...

  6. 线程的同步机制:同步代码块&同步方法

    解决存在的线程安全问题:打印车票时出现重票,错票 使用同步代码块的解决方案 TestWindow2 package com.aff.thread; /* 使用实现Runnable接口的方式,售票 存在 ...

  7. 7z命令行简单使用

    7z命令行简单使用 网上有很多博客都有记录7z的命令行使用方式,但看起来乱起八糟的,不知所云. 急于使用者可以直接看实例 注:我仅仅记录我认为常用的命令,毕竟没有那么多的精力去学习不常用的东西. 简介 ...

  8. & 异步使用场景

    异步的使用场景: 1.不涉及共享资源,或对共享资源只读,即非互斥操作 2.没有时序上的严格关系 3.不需要原子操作,或可以通过其他方式控制原子性 4.常用于IO操作等耗时操作,因为比较影响客户体验和使 ...

  9. 01 . RabbitMQ简介及部署

    RabbitMQ简介 ​ MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它 ...

  10. Java实现 LeetCode 826 安排工作以达到最大收益(暴力DP)

    826. 安排工作以达到最大收益 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[i]是第i个工人的能力,即该 ...