UVa1382--Distant Galaxy(离散化+扫描线)

题目大意:在平面内给出n个点,找一个矩形使得各边包含尽量多的点。


分析:枚举上下边界,再确定左右边界。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 111;

struct Point {
    int x, y;
    bool operator< (const Point& cmp) const{
        return x < cmp.x;
    }
}p[maxn];

int n, m, y[maxn], on[maxn], on2[maxn], left0[maxn];

int solve() {
    sort(p, p+n);
    sort(y, y+n);
    m = unique(y, y+n)-y;
    if(m <= 2) return n;
    int ans = 0;
    for(int a = 0; a < m; a++) {
        for(int b = a+1; b < m; b++) {
            int k = 0;
            for(int i = 0; i < n; i++) {
                if(i == 0 || p[i].x != p[i-1].x) {
                    k++;
                    on[k] = on2[k] = 0;
                    left0[k] = left0[k-1]+on2[k-1]-on[k-1];
                }
                if(y[a] <= p[i].y && p[i].y <= y[b]) on2[k]++;
                if(y[a] < p[i].y && p[i].y < y[b]) on[k]++;
            }
            if(k <= 2) return n;
            int M = 0;
            for(int j = 1; j <= k; j++) {
                ans = max(ans, left0[j]+on2[j]+M);
                M = max(M, on[j]-left0[j]);
            }
        }
    }
    return ans;
}

int main() {
    int kase = 1;
    while(scanf("%d", &n) && n != 0) {
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &p[i].x, &p[i].y);
            y[i] = p[i].y;
        }
        printf("Case %d: %d\n", kase++, solve());
    }
    return 0;
}


你可能感兴趣的:(UVa1382--Distant Galaxy(离散化+扫描线))