The largest sub-rectangle problem

Gangster blog to explain
P1578 cow bathing
Title Description
Due John built a cattle farm fence, aroused the anger of cows, milk production sharply. In order to curry favor with cows, John decided to build a large cattle farm in the bath. But John's cows have a strange habit, each cow must be a fixed position in milk production in cattle, and cow milk is clearly not in the bath, then, hope baths built by John does not cover these points milk . This time, he has to resort to the Clevow. You can also help Clevow it?

Baths cattle and John are planning a rectangle. Baths To completely within the farm, and parallel to bath or contour coincides with the contour of the cattle. Baths milk can not cover any point, but may be located on the contour point milk baths.

Clevow certainly hope bathing area as large as possible, so your task is to help her calculate the maximum area of ​​the baths.

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 +7;
typedef long long ll;
struct node {
    int x, y;
    bool operator < (node& a) const {
        if(x == a.x) return y < a.y;
        return x < a.x;
    }
}p[maxn];
bool cmp(node a, node b) {
    if(a.y == b.y) return a.x < b.x;
    return a.y < b.y;
}
int main()
{
    int L, W, n;
    scanf("%d%d%d", &L, &W, &n);
    for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y);
    p[++n].x = 0, p[n].y = 0;
    p[++n].x = L, p[n].y = 0;
    p[++n].x = 0, p[n].y = W;
    p[++n].x = L, p[n].y = W;
    sort(p + 1, p + 1 + n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {//从左向右扫
        int h = W, l = 0;
        for (int j = i + 1; j <= n; j++) {
            ans = max(ans, (p[j].x - p[i].x) * (h - l));
            if(p[j].y < p[i].y) l = max(l, p[j].y);
            else h = min(h, p[j].y);
        }
    }
    for (int i = n; i >= 1; i--) {//从右向左扫
        int h = W, l = 0;
        for (int j = i - 1; j >= 1; j--) {
            ans = max(ans, (p[i].x - p[j].x) * (h - l));
            if(p[j].y < p[i].y) l = max(l, p[j].y);
            else h = min(h, p[j].y);
        }
    }
    sort(p + 1, p + 1 + n, cmp);//枚举遗漏的情况
    for (int i = 1; i <= n - 1; i++) ans = max(ans, (p[i + 1].y - p[i].y) * L);
    printf("%d\n", ans);
    return 0;
}

Published 25 original articles · won praise 2 · Views 268

Guess you like

Origin blog.csdn.net/D_Bamboo_/article/details/104072705