luogu4147(DP+单调栈)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/83184673

这个是求不包含0的最大子矩阵的面积

然后可以这么考虑,先用dp求出以每个点为右下角的最大的正方形,然后对每行扫过去,用单调栈维护,当每个元素出栈的时候,和他的最大正方形相同边长的元素必定出栈,那么此时就求以这个边长为宽的面积,然后长可以用最前面和他相同的元素的下标和当前下标求得。。

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)>>1)
#define NM 2005
#define nm 2005
#define pi 3.1415926535897931
const int inf=998244353;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}











int n,m,a[NM][NM],d[NM][NM],ans;
stack<int>s;
char _s[5];

int main(){
    n=read();m=read();
    inc(i,1,n)inc(j,1,m){
    scanf("%s",_s);
    a[i][j]=_s[0]=='F';
    }
    inc(i,1,n)inc(j,1,m)if(a[i][j])d[i][j]=min(d[i-1][j-1],min(d[i-1][j],d[i][j-1]))+1;
    //inc(i,1,n){inc(j,1,m)printf("%d ",d[i][j]);putchar('\n');}
    inc(i,1,n){
    while(!s.empty())s.pop();s.push(0);
    inc(j,1,m+1){
        while(!s.empty()&&d[i][s.top()]>d[i][j]){
        int r=s.top(),l=s.top();
        while(!s.empty()&&d[i][r]==d[i][s.top()])l=s.top(),s.pop();
        ans=max(ans,d[i][r]*(j-s.top()+d[i][r]-2));
        }
        s.push(j);
    }
    }
    inc(j,1,m){
    while(!s.empty())s.pop();s.push(0);
    inc(i,1,n+1){
        while(!s.empty()&&d[s.top()][j]>d[i][j]){
        int r=s.top(),l=s.top();
        while(!s.empty()&&d[r][j]==d[s.top()][j])l=s.top(),s.pop();
        ans=max(ans,d[r][j]*(i-s.top()+d[r][j]-2));
        }
        s.push(i);
    }
    }
    return 0*printf("%d\n",ans*3);
}

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/83184673
今日推荐