[lnsyoj103/luoguP4147]玉蟾宫

题意

给定一个由 FR 组成的矩阵 \(a\),求 \(a\) 中最大的只由 F 组成的矩形的面积的三倍

sol

求最大矩形的常用方法为悬线法
首先,对于每一个 F 使用递推法计算出上方连续的 F 的数量,记为矩阵 \(h\),然后对 \(h\) 的每一行计算每一个元素左右最远能延伸的距离,即该元素左右第一个小于该元素的值的位置,分别记为矩阵 \(l, r\),那么,最终的答案即为 \(\max\{h_{i,j} (r_{i,j} - l_{i,j} - 1)\} \times 3\)
计算 \(l\)\(r\) 可参考[lnsyoj102/luoguP2866]Bad Hair Day

代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 1005;

int h[N][N], l[N][N], r[N][N];
int stk[N], top;
int n, m;

int main(){
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ ){
        for (int j = 1; j <= m; j ++ ){
            char op[2];
            scanf("%s", op);
            if (*op == 'F') h[i][j] = h[i - 1][j] + 1;
        }

        top = 0;
		stk[0] = 0;
        for (int j = 1; j <= m; j ++ ){
            while (top && h[i][j] <= h[i][stk[top]]) top -- ;
            l[i][j] = stk[top];
            stk[ ++ top] = j;
        }

        top = 0;
        stk[0] = m + 1;
        for (int j = m; j; j -- ){
            while (top && h[i][j] <= h[i][stk[top]]) top -- ;
            r[i][j] = stk[top];
            stk[ ++ top] = j;
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; i ++ ){
        for (int j = 1; j <= m; j ++ ) ans = max(ans, h[i][j] * (r[i][j] - l[i][j] - 1));
    }
    printf("%d\n", ans * 3);

    return 0;
}
posted @ 2024-07-20 10:08  是一只小蒟蒻呀  阅读(5)  评论(0编辑  收藏  举报