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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章