City game (monotonous stack)

Title:
One day, kittens and rainbow freda came to Hunan Zhangjiajie Tianmen Mountain jade moon, jade moon Miyaji Blue Rabbit kindly entertain them, and gave them a piece of land.

This land is divided into N * M grid, the grid is written each 'R' or 'F', R was given on behalf of the land of the rainbow, F being given on behalf of the land freda.

Now freda here to sell Meng. . . It is looking for a rectangular land, the land requirements are marked with 'F' and the largest area.

But the level of OI and rainbow freda are weak burst, can not find the land, but also want to see freda Blue Rabbit sold Meng (she is obviously not programmed ......), so they decided that if you find an area of ​​land S, they will give you 3 * S ounces of silver.

Input format
of the first row comprises two integers N, M, represents a rectangular land has N rows and M columns.

Next N lines of M space-separated character 'F' or 'R', rectangular land is described.

End of each line no extra spaces.

Output format
output an integer that represents how much money you can get, namely (maximum 3 * 'F' rectangle of land area) values.

Data range
1≤N, M≤1000
input sample column:
. 5. 6
RFFFFF
FFFFFF
RRRFFF
FFFFFF
FFFFFF
output:
45
Link
Analysis: can be seen as a histogram, where pre-processing it, the behavior of every x coordinate, build a histogram, then I spoke on a solution to a problem , ask the same law, more than a step to enumerate each act x-axis; pretreatment, then the middle if there is a broken, you scratch;
the following function f is a function and get a direct request histogram largest rectangular area to move over, changed little, and I put them apart.
c ++ code is as follows:

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#pragma G++ optimize(2)
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const double eps = 1e-5;
const int mod = 999911659;
const int N = 1010;
typedef pair<int,int> pii;
char e[N][N];
int n,m,h[N][N];
int br[N],bl[N];
stack<int> sal,sar;
void get(int x)
{
	while(sar.size()) sar.pop();
	while(sal.size()) sal.pop();
	for(int i=1;i<=m;i++)
	{
		//求右边
		while(sar.size() && h[x][sar.top()] >= h[x][i])
		{
			br[sar.top()]=i;
			sar.pop();
		}
		sar.push(i);
		//求左边
		while(sal.size() && h[x][sal.top()] >= h[x][i])
		sal.pop();
		if(sal.size()) bl[i]=sal.top();
		sal.push(i);
	}
	return ;
}
ll f(int x)
{
	memset(br,0,sizeof br);
	memset(bl,0,sizeof bl);
	get(x);
	ll res=0,ma=0;
	//枚举找到每一行中最大的矩形面积
	for(int i=1;i<=m;i++)
	{
		if(br[i]==0) br[i]=m+1;
		res=h[x][i]*(br[i]-bl[i]-1ll);//1ll转换成ll型
		ma=max(ma,res);
	}
	return ma;
}
int main(){
	IOS;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			cin>>e[i][j];
			//预处理
			if(e[i][j]=='F') h[i][j]=h[i-1][j]+1;
			else h[i][j]=0;
		}
	ll ma=0;
	//枚举每一行;
	for(int i=1;i<=n;i++)
	ma=max(ma,f(i));
	cout<<ma*3<<endl;
	return 0;
}

There are also questions please contact;

Released six original articles · won praise 9 · views 116

Guess you like

Origin blog.csdn.net/qq_45604735/article/details/104581715