【单调栈】 城市游戏

题意

传送门
\(N\times M\)矩阵,每个格子为\(R\)\(F\),求面积\(S\)最大的全部为\(F\)的字矩阵,输出\(3\times S\)

数据范围

\(1\leq N\leq 1000\)
\(1\leq M\leq 1000\)

题解

枚举每一行上每一列的点上的\(F\)高度,面积求法同直方图中的最大矩形,时间复杂度为\(O(NM)\)
小区别是当前行中当前列没有\(F\)即高度为0,不会对后面产生影响因为是断开的

Code

#include
using namespace std;
#define rep(i,a,n) for(int i=a;istk[t]){
            stk[++t]=h[j];
            wid[t]=1;
        }
        else{
            int width=0;
            while(stk[t] >h[j]){
                width+=wid[t];
                res=max(res,width*stk[t]);
                t--;
            }
            stk[++t]=h[j];
            wid[t]=width+1;
        }
    }
    return res;
}   
int main(){
    char c;
    cin>>n>>m;
    rep(i,0,n) rep(j,0,m) {
        cin>>c;
        a[i][j] = (c=='F')?1:0;
    }
    int ans=0;
    rep(i,0,n) ans=max(ans,solve(i));
    cout<

你可能感兴趣的:(【单调栈】 城市游戏)