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
#include
#include
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 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<

 

你可能感兴趣的:(2019牛客暑期多校训练营)