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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| // 42jerrykim.github.io에서 더 많은 정보를 확인 할 수 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct Point {
long long x, y;
bool operator<(const Point& other) const {
if (x != other.x) return x < other.x;
return y < other.y;
}
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
};
static inline Point operator-(const Point& a, const Point& b) {
return {a.x - b.x, a.y - b.y};
}
static inline long long cross(const Point& a, const Point& b) {
return a.x * b.y - a.y * b.x;
}
static inline long long cross(const Point& a, const Point& b, const Point& c) {
// (b-a) x (c-a)
return cross(b - a, c - a);
}
static inline long long dist2(const Point& a, const Point& b) {
long long dx = a.x - b.x;
long long dy = a.y - b.y;
return dx * dx + dy * dy;
}
vector<Point> convexHull(vector<Point> pts) {
sort(pts.begin(), pts.end());
pts.erase(unique(pts.begin(), pts.end()), pts.end());
int n = (int)pts.size();
if (n <= 1) return pts;
vector<Point> lower, upper;
lower.reserve(n);
upper.reserve(n);
for (int i = 0; i < n; i++) {
while ((int)lower.size() >= 2 &&
cross(lower[(int)lower.size() - 2], lower.back(), pts[i]) <= 0) {
lower.pop_back();
}
lower.push_back(pts[i]);
}
for (int i = n - 1; i >= 0; i--) {
while ((int)upper.size() >= 2 &&
cross(upper[(int)upper.size() - 2], upper.back(), pts[i]) <= 0) {
upper.pop_back();
}
upper.push_back(pts[i]);
}
lower.pop_back();
upper.pop_back();
lower.insert(lower.end(), upper.begin(), upper.end()); // CCW hull
return lower;
}
long long diameterSquared(const vector<Point>& hull) {
int m = (int)hull.size();
if (m <= 1) return 0;
if (m == 2) return dist2(hull[0], hull[1]);
auto area2 = [&](int i, int ni, int k) -> long long {
return llabs(cross(hull[i], hull[ni], hull[k]));
};
long long ans = 0;
int j = 1;
for (int i = 0; i < m; i++) {
int ni = (i + 1) % m;
while (area2(i, ni, (j + 1) % m) > area2(i, ni, j)) {
j = (j + 1) % m;
}
ans = max(ans, dist2(hull[i], hull[j]));
ans = max(ans, dist2(hull[ni], hull[j]));
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<Point> pts;
pts.reserve(N);
for (int i = 0; i < N; i++) {
long long x, y;
cin >> x >> y;
pts.push_back({x, y});
}
vector<Point> hull = convexHull(pts);
cout << diameterSquared(hull) << '\n';
return 0;
}
|