POJ2187 Beauty Contest(凸包+对踵点对求解)

思路:求凸包,然后旋转卡壳对最远点对求解,注意凸包退化的情况

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
const int maxn = 1e5 + 10;
using namespace std;

struct P {
    int x, y;
    P() {}
    P(int x, int y) : x(x), y(y) {}
    P operator - (P p) {
        return P(x - p.x, y - p.y);
    }
    int det(P a) {
        return x * a.y - y * a.x;
    }
    int dist(P c) {
        int X = c.x - x;
        int Y = c.y - y;
        return (X * X + Y * Y);
    }
    void input() {
        scanf("%d %d", &x, &y);
    }
};

P po[maxn], C[maxn];
int n, k;

bool cmp_x(P a, P b) {
    if(a.x != b.x) return a.x < b.x;
    return a.y < b.y;
}
//凸包求解
void convex_hull() {
    k = 0;
    sort(po, po + n, cmp_x);
    for(int i = 0; i < n; i++) {
        while(k > 1 && (C[k - 1] - C[k - 2]).det(po[i] - C[k - 1]) <= 0) k--;
        C[k++] = po[i];
    }
    for(int i = n - 2, t = k; i >= 0; i--) {
        while(k > t && (C[k - 1] - C[k - 2]).det(po[i] - C[k - 1]) <= 0) k--;
        C[k++] = po[i];
    }
    k--;
}
//旋转卡壳 -- 对踵点对求解
int solve() {
    //凸包退化
    if(k == 2) return C[0].dist(C[1]);
    int i = 0, j = 0;
    for(int kk = 0; kk < k; kk++) {
        if(!cmp_x(C[i], C[kk])) i = kk;
        if(cmp_x(C[j], C[kk])) j = kk;
    }
    int res = 0;
    int si = i, sj = j;
    while(si != j || sj != i) {
        res = max(res, C[i].dist(C[j]));
        if((C[(i + 1) % k] - C[i]).det(C[(j + 1) % k] - C[j]) < 0) i = (i + 1) % k;
        else j = (j + 1) % k;
    }
    return res;
}

int main() {
    while(scanf("%d", &n) != EOF) {
        for(int i = 0; i < n; i++)
            po[i].input();
        convex_hull();
        int res = solve();
        printf("%d\n", res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值