2019牛客暑期多校训练营(第二场)- H Second Large Rectangle

时间:2019-07-20
本文章向大家介绍2019牛客暑期多校训练营(第二场)- H Second Large Rectangle,主要包括2019牛客暑期多校训练营(第二场)- H Second Large Rectangle使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

单调栈

单调栈找出每以每个点所在的行为底能够产生的最大矩形面积,然后放进优先队列。

要找的是次大值,所以可能他的子矩阵也是我们要的答案,所有还有把当前矩阵长-1,宽-1的面积也放进去。

最后去重,对于相同的面积,我们在保存左右边界,这样矩形的长被确定了,所以宽也被确定了,这个矩形就被确定了,我们可以通过比较左右边界是否相同来判断是否出队的是同一个矩形,如果是就不管它,不是则记录答案。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int ret = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
    return w ? -ret : ret;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template <typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template <typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 1005;
int h[N][N], l[N], r[N], g[N][N];
string s[N];
struct Node{
    int val, l, r;
    Node(){}
    Node(int val, int l, int r): val(val), l(l), r(r){}
    bool operator < (const Node &rhs) const {
        //if(val != rhs.val) return val < rhs.val;
        //return l > rhs.l;
        return val < rhs.val;
    }
};

int main(){

    int n, m;
    while(cin >> n >> m){
        priority_queue<Node> pq;
        full(h, 0), full(l, 0), full(r, 0);
        for(int i = 0; i < n; i ++) cin >> s[i];
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++) g[i][j] = s[i - 1][j - 1] - '0';
        }
        for(int j = 1; j <= m; j ++) h[1][j] = g[1][j] == 1 ? 1 : 0;
        for(int i = 2; i <= n; i ++){
            for(int j = 1; j <= m; j++) h[i][j] = g[i][j] == 0 ? 0 : h[i - 1][j] + 1;
        }
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++) l[j] = r[j] = j;
            for(int j = 2; j <= m; j ++){
                while(l[j] > 1 && h[i][j] <= h[i][l[j] - 1]) l[j] = l[l[j] - 1];
            }
            for(int j = m - 1;j >= 1; j --){
                while(r[j] < m && h[i][j] <= h[i][r[j] + 1]) r[j] = r[r[j] + 1];
            }
            for(int j = 1; j <= m; j ++){
                int s = (r[j] - l[j] + 1) * h[i][j];
                int x = r[j] - l[j] + 1, y = h[i][j];
                //cout << i << " " << j << " " << l[j] << " " << r[j] << endl;
                pq.push(Node(s, l[j], r[j])), pq.push(Node((x - 1) * y, l[j], r[j])), pq.push(Node(x * (y - 1), l[j], r[j] - 1));
            }
        }
        int cnt = 0, lx = 0, ly = 0, lval = 0;
        while(!pq.empty()){
            Node cur = pq.top(); pq.pop();
            //cout << cur.val << " " << cur.l << " " << cur.r << endl;
            if(lval == cur.val){
                if(cur.l != lx || cur.r != ly){
                    if(cnt == 1){
                        printf("%d\n", cur.val);
                        break;
                    }
                    else cnt ++, lx = cur.l, ly = cur.r;
                }
                else continue;
            }
            else{
                if(cnt == 1){
                    printf("%d\n", cur.val);
                    break;
                }
                else cnt ++, lx = cur.l, ly = cur.r, lval = cur.val;
            }
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/onionQAQ/p/11219164.html