[BZOJ3039]玉蟾宫

3039: 玉蟾宫

Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 920  Solved: 532 [Submit][Status][Discuss]

Description

有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地。 这片土地被分成N*M个格子,每个格子里写着'R'或者'F',R代表这块土地被赐予了rainbow,F代表这块土地被赐予了freda。 现在freda要在这里卖萌。。。它要找一块矩形土地,要求这片土地都标着'F'并且面积最大。 但是rainbow和freda的OI水平都弱爆了,找不出这块土地,而蓝兔也想看freda卖萌(她显然是不会编程的……),所以它们决定,如果你找到的土地面积为S,它们每人给你S两银子。

Input

第一行两个整数N,M,表示矩形土地有N行M列。 接下来N行,每行M个用空格隔开的字符'F'或'R',描述了矩形土地。

Output

输出一个整数,表示你能得到多少银子,即(3*最大'F'矩形土地面积)的值。

Sample Input

5 6
R F F F F F
F F F F F F
R R R F F F
F F F F F F
F F F F F F

Sample Output

45

HINT

对于50%的数据,1<=N,M<=200
对于100%的数据,1<=N,M<=1000

求最大子矩阵

悬线法,单调栈

#pragma GCC optimize("O2")
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[2100000], *ptr = buf - 1;
inline int readint(){
    int n = 0;
    char ch = *++ptr;
    while(ch < '0' || ch > '9') ch = *++ptr;
    while(ch <= '9' && ch >= '0'){
        n = (n << 1) + (n << 3) + (ch ^ 48);
        ch = *++ptr;
    }
    return n;
}
const int maxn = 1000 + 10;
int n, m, ans = 0;
int top[maxn] = {0};
int s[maxn], l[maxn], t = 0;
inline void work(){
    int len;
    for(int i = 1; i <= m; i++){
        if(!t || top[i] > top[s[t]]){
            s[++t] = i;
            l[t] = 1;
        }
        else{
            len = 0;
            while(t && top[s[t]] >= top[i]){
                len += l[t];
                ans = max(ans, len * top[s[t]]);
                t--;
            }
            s[++t] = i;
            l[t] = len + 1;
        }
    }
    len = 0;
    while(t){
        len += l[t];
        ans = max(ans, len * top[s[t]]);
        t--;
    }
}
int main(){
    fread(buf, sizeof(char), sizeof(buf), stdin);
    n = readint();
    m = readint();
    char ch;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            ch = *++ptr;
            while(ch != 'F' && ch != 'R') ch = *++ptr;
            if(ch == 'F') top[j]++;
            else top[j] = 0;
        }
        work();
    }
    printf("%d
", ans * 3);
    return 0;
}
原文地址:https://www.cnblogs.com/ruoruoruo/p/7625831.html