[洛谷P4147]玉蟾宮
阿新 • • 發佈:2018-11-07
題目大意:最大全一子矩陣
題解:單調棧
卡點:果然我還是不會單調棧,長寬寫錯
C++ Code:
#include <cstdio> #define maxn 1010 struct node { int len, h; inline node (int __len = 0, int __h = 0) {len = __len, h = __h;} } S[maxn]; inline int max(int a, int b) {return a > b ? a : b;} int top; int n, m, ans; char p[maxn][maxn]; int s[maxn][maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%1s", p[i] + j); if (p[i][j] == 'F') s[i][j] = s[i - 1][j] + 1; } } for (int i = 1; i <= n; i++) { S[top = 1] = node(1, s[i][1]); for (int j = 2; j <= m; j++) { int len = 0; while (top && s[i][j] <= S[top].h) { len += S[top].len; ans = max(ans, len * S[top].h); top--; } S[++top] = node(len + 1, s[i][j]); } int len = 0; while (top) { len += S[top].len; ans = max(ans, len * S[top].h); top--; } } printf("%d\n", ans * 3); return 0; }