【最大子矩阵】 城市游戏

传送门

题意

\(N\times M\)矩阵,每个格子为\(R\)\(F\),求面积最大的全部为\(F\)的子矩阵,输出\(3\times S\)

数据范围

\(1\leq N\leq 1000\)
\(1\leq M\leq 1000\)

题解

  • 对每一行,求出每一列 \(F\) 的高度,面积求法同直方图中的最大矩形,时间复杂度为\(O(NM)\)

  • 区别是当前行中当前列没有 \(F\) 即高度为0,断开了

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i = a; i < n; i ++)
const int N = 1010;
int n, m;
bool a[N][N];
int stk[N], t;
int h[N], wid[N];

int solve(int i) {
    int res = 0;
    t = 0;
    rep(j, 0, m)
        if(a[i][j]) h[j]++;
        else h[j] = 0;
        
    rep(j, 0, m + 1) {
        if(h[j] > stk[t]) {
            stk[++t] = h[j];
            wid[t] = 1;
        }
        else {
            int width = 0;
            while(stk[t] > h[j]) {
                width += wid[t];
                res = max(res, width * stk[t]);
                t--;
            }
            stk[++t] = h[j];
            wid[t] = width + 1;
        }
    }
    return res;
}   
int main() {
    char c;
    cin>>n>>m;
    rep(i, 0, n) rep(j, 0, m) {
        cin>>c;
        a[i][j] = (c == 'F') ? 1 : 0;
    }
    int ans = 0;
    rep(i, 0, n) ans = max(ans, solve(i));
    cout<<ans * 3<<endl;
}
posted @ 2020-07-11 16:44  Hyx'  阅读(132)  评论(0编辑  收藏  举报