杂题训练之二

https://www.luogu.org/problem/P4147

其实和最大子矩阵的思路特别像

可以用悬线法,也可以用单调栈,

因为前文我写过悬线法,所有,这里就用单调栈

因为单调栈是模板,,手动模拟一下就可以写出来的

所以不提供讲解只提供代码
code:

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int a[1001][1001];
int s[1001],l[1001];
int n,m,x,ans=0;
void work(int h[])
{
     int top=0,len=0;
     s[top]=0;l[top]=0;
     for(int i=1;i<=m;i++){
        if(h[i]>=s[top])s[++top]=h[i],l[top]=1;
        else{len=0;
            while(top&&s[top]>h[i])len+=l[top],ans=max(ans,len*s[top]),top--;
        s[++top]=h[i];l[top]=len+1;
            }
    }
     len=0;
     while(top)len+=l[top],ans=max(ans,len*s[top]),top--;      
 }
int main()
{
    int p;char ch[1];
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
       for(int j=1;j<=m;j++){scanf("%s",ch);
          if(ch[0]=='F')a[i][j]=a[i-1][j]+1;
       }
    for(int i=1;i<=n;i++)work(a[i]);
    printf("%d",3*ans);
return 0;
}

猜你喜欢

转载自www.cnblogs.com/wzxbeliever/p/11622978.html