A22708.玉蟾宫
2025-08-04 16:58:42
发布于:浙江
6阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+100;
int main(){
int N, M;
cin >> N >> M;
// h[0] 和 h[M+1] 作为高度为 0 的“哨兵”
vector<int> h(M + 2, 0);
int maxS = 0;
for (int i = 0; i < N; i++) {
// —— 1. 更新高度数组 h ——
for (int j = 1; j <= M; j++) {
char c;
cin >> c;
if (c == 'F')
h[j] += 1; // 上面继续是 F,则高度+1
else
h[j] = 0; // 遇到 R,高度清零
}
// —— 2. 单调栈求当前直方图最大矩形 ——
stack<int> stk;
stk.push(0); // 先把左端哨兵 (下标 0) 入栈
// 遍历到 M+1 保证清空所有柱子
for (int j = 1; j <= M+1; j++) {
// 当当前高度小于栈顶高度,弹出并计算面积
while (!stk.empty() && h[j] < h[stk.top()]) {
int tp = stk.top();
stk.pop();
int height = h[tp];
// 弹出后新的栈顶就是左边界
int left = stk.top();
// 右边界是当前位置 j
int width = j - left - 1;
maxS = max(maxS, height * width);
}
stk.push(j);
}
}
// 最后乘 3 输出
cout << 3LL * maxS << "\n";
}
这里空空如也
有帮助,赞一个