题意
给定一个由 F
和 R
组成的矩阵 \(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;
}
标签:玉蟾,int,矩阵,luoguP4147,lnsyoj103,include
From: https://www.cnblogs.com/XiaoJuRuoUP/p/-/lnsyoj103_luoguP4147