做题记录整理栈5 #P4147. 玉蟾宫(2022/9/15)

#P4147. 玉蟾宫

本质上是一个二维的最大刷墙
我们把他按照每一行切开看,就会发现它变成了一个最大刷墙序列
然后就直接做就得了,预处理O(nm),算出答案O(mn)

#include<bits/stdc++.h>
#define for1(i,a,b) for(int i = a;i<=b;i++)
#define ll long long
#define mp(a,b) make_pair(a,b)
using namespace std;
int b[5005][5005],n,m,ji[5005][5005];
struct node
{
	int len,h;
}a[5005];
int mx;
stack<node> s;
void cl(int x)
{
		memset(a,0,sizeof(a));
	a[1].h=ji[x][1],a[1].len=1;
	while(!s.empty())  s.pop();
	
	s.push(a[1]);
	
	for1(i,2,m)
	{
		int w=0;
		while(s.size()&&ji[x][i]<=s.top().h)
		{
			w+=s.top().len;
			mx=max(mx,w*s.top().h);
			s.pop();
		}
		a[i].h=ji[x][i];
		a[i].len=w+1;
		s.push(a[i]);
	}
	int w=0;
	while(!s.empty())
	{
		w+=s.top().len;
		mx=max(mx,s.top().h*w);
		s.pop();
	}
}
int main()
{
    char x;
	cin>>n>>m;;
	for1(i,1,n)
	for1(j,1,m)
	scanf("%s",&x),b[i][j]=(x=='F'?1:0); 
	for1(i,1,n)
	for1(j,1,m)
		if(b[i][j]!=0) ji[i][j]=ji[i-1][j]+1;
	
	for1(i,1,n)
		cl(i);
	cout<<mx*3;
	return 0;
}
posted @ 2022-09-15 20:25  yyx525jia  阅读(24)  评论(0)    收藏  举报