Beauty Contest

题目描述

Bessie, Farmer John’s prize cow, has just won first place in a bovine beauty contest, earning the title ‘Miss Cow World’. As a result, Bessie will make a tour of $N$ farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates $(x, y)$, each having a value in the range $-10000 \ldots 10000$. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring. Help Bessie by computing the maximum distance among all pairs of farms.

题意概述

求$N$个点中最远点对之间的距离。

数据范围:$2 \le N \le 50000$。

算法分析

显然最远点对一定在凸包上,因此可以先计算出凸包。接着考虑旋转卡壳。被一对卡壳正好卡住的点对互为对踵点,那么最远点对一定互为对踵点。只要将卡壳绕凸包旋转一周,就可以找到所有对踵点对,取其中距离最远的点对即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdio>
#include <algorithm>

int max(int a, int b) {
return a > b ? a : b;
}

static const int N = 50005;
int st[N], rec[N];
struct Point {
int x, y;
Point(int _x = 0, int _y = 0) : x(_x), y(_y) {}
bool operator < (const Point &p) const {
return x == p.x ? y < p.y : x < p.x;
}
Point operator - (const Point &p) const {
return Point(x - p.x, y - p.y);
}
int operator * (const Point &p) const {
return x * p.y - y * p.x;
}
int operator ! () const {
return x * x + y * y;
}
} p[N];

int main() {
int n; scanf("%d", &n);
for (int i = 0; i < n; ++ i) scanf("%d%d", &p[i].x, &p[i].y);
int top = 0, tot = 0; std :: sort(p, p + n);
for (int i = 0; i < n; ++ i) {
while (top > 1 && (p[st[top - 1]] - p[st[top - 2]]) * (p[i] - p[st[top - 2]]) <= 0) -- top;
st[top ++] = i;
}
for (int i = 0; i < top; ++ i) rec[tot ++] = st[i];
top = 0;
for (int i = n - 1; ~ i; -- i) {
while (top > 1 && (p[st[top - 1]] - p[st[top - 2]]) * (p[i] - p[st[top - 2]]) <= 0) -- top;
st[top ++] = i;
}
for (int i = 1; i < top - 1; ++ i) rec[tot ++] = st[i];
int mx = 0, pnt = 1;
for (int i = 0; i < tot; ++ i) {
int nxt = (i + 1) % tot; Point cur = p[rec[nxt]] - p[rec[i]];
while (cur * (p[rec[pnt]] - p[rec[i]]) < cur * (p[rec[(pnt + 1) % tot]] - p[rec[i]])) (++ pnt) %= tot;
mx = max(mx, max(! (p[rec[pnt]] - p[rec[i]]), ! (p[rec[pnt]] - p[rec[nxt]])));
}
printf("%d\n", mx);
return 0;
}

Beauty Contest
https://regmsif.cf/2018/01/12/oi/beauty-contest/
作者
RegMs If
发布于
2018年1月12日
许可协议