poj2187 Beauty Contest 平面最远点对(凸包)

题意:

给你n个点,问距离最远的两个点的距离的平方是多少。

思路:

最远点对一定在凸包上,所以枚举凸包上的点即可。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

#define MAXN 50005

using namespace std;

struct Point{
    long long x, y;
    Point() {}
    Point(long long a, long long b) : x(a), y(b) {}
};

int n;
Point p[MAXN];
Point stack[MAXN << 1];
int top = 0;

long long dist(Point a, Point b) {
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

long long multi(Point a, Point b, Point c) {
    return (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x);
}

int cmp(Point a, Point b) {
    long long t = multi(p[0], a, b);
    if (!t) return dist(p[0], a) < dist(p[0], b);
    return t > 0;
}

void graham_scan() {
    int t = 0;
    for (int i = 1; i < n; i++) {
        if (p[i].y < p[t].y || (p[i].y == p[t].y && p[i].x < p[t].x))
            t = i;
    }
    swap(p[0], p[t]);
    sort(p + 1, p + n, cmp);
    stack[top++] = p[0];
    stack[top++] = p[1];
    stack[top++] = p[2];
    for (int i = 3; i < n; i++) {
        while (multi(stack[top - 2], stack[top - 1], p[i]) < 0) {
            top--;
        }
        stack[top++] = p[i];
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++)
        scanf("%lld %lld", &p[i].x, &p[i].y);
    graham_scan();
    for (int i = top; i < top + top; i++)
        stack[i] = stack[i - top];
    n = top << 1;
    int t = 0;
    long long best = -0x3f3f3f3f;
    for (int i = 0; i < top; i++) {
        for (int j = i + 1; j < top; j++) {
            best = max(best, dist(stack[i], stack[j]));
        }
    }
    cout << best << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值