2019牛客暑期多校训练营(第二场)H Second Large Rectangle

题目链接:https://ac.nowcoder.com/acm/contest/882/H

题目描述

Given a N×M

binary matrix. Please output the size of second large rectangle containing all "1".

Containing all "1" means that the entries of the rectangle are all "1".
 

A rectangle can be defined as four integers x1,y1,x2,y2

where 1≤x1≤x2≤N and 1≤y1≤y2≤M. Then, the rectangle is composed of all the cell (x, y) where x1≤x≤x2 and y1≤y≤y2. If all of the cell in the rectangle is "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 cij

.

1≤N,M≤1000
N×M≥2
cij∈"01"
 

 

输出描述:

 

Output one line containing an integer representing the answer. If there are less than 2 rectangles containning all "1"

, output "0"
 

.

示例1

输入

复制

1 2
01

输出

复制

0

示例2

输入

复制

1 3
101

输出

复制

1

题面大意:

给出一个由0和1组成的一个矩阵,求出出这个矩阵中全部由1组成的第二大的子矩阵

需要使用单调栈

对矩阵进行处理, 转化成一个直方图,然后套用单调栈即可

代码:

#include<iostream>
#include<stdio.h>
#include<stack>
using namespace std;

int max1=0,max2=0;
int a[1010][1010];
char b[1010];

void check(int n){
	if(n>max1){
		int t=max1;
		max1=n;
		max2=t;
	}
	else if(n>max2){
		max2=n;
	}
}

void Area(int x,int y){
	check(x*y);
	check((x-1)*y);
	check(x*(y-1));
}

int main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>b;
		for(int j=1;j<=m;j++){
			a[i][j]=b[j-1]-'0';
			if(a[i][j]==1){
				a[i][j]+=a[i-1][j];
			}
		}
	}
	for(int i=1;i<=n;i++){
		a[i][0]=-2;
		a[i][m+1]=-1;
		stack<int> q;
		q.push(0);
		for(int j=1;j<=m+1;j++){
			while(a[i][q.top()]>a[i][j]){
				int index=q.top();
				q.pop();
				Area(j-q.top()-1,a[i][index]);
			}
			q.push(j);
		}
	}
	cout<<max2<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值