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
| // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
struct Point {
long long x, y;
bool operator<(const Point& o) const {
if (x != o.x) return x < o.x;
return y < o.y;
}
bool operator==(const Point& o) const { return x == o.x && y == o.y; }
};
static inline long long cross(const Point& a, const Point& b, const Point& c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
static inline long long dist2(const Point& a, const Point& b) {
long long dx = a.x - b.x, 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;
for (const auto& p : pts) {
while ((int)lower.size() >= 2 && cross(lower[(int)lower.size()-2], lower.back(), p) <= 0) lower.pop_back();
lower.push_back(p);
}
for (int i = n - 1; i >= 0; --i) {
const auto& p = pts[i];
while ((int)upper.size() >= 2 && cross(upper[(int)upper.size()-2], upper.back(), p) <= 0) upper.pop_back();
upper.push_back(p);
}
lower.pop_back();
upper.pop_back();
lower.insert(lower.end(), upper.begin(), upper.end());
return lower;
}
pair<Point, Point> diameterRotatingCalipers(const vector<Point>& h) {
int m = (int)h.size();
if (m == 1) return {h[0], h[0]};
if (m == 2) return {h[0], h[1]};
long long best = -1;
pair<int,int> ans = {0, 0};
int j = 1;
for (int i = 0; i < m; ++i) {
int ni = (i + 1) % m;
while (true) {
int nj = (j + 1) % m;
long long cur = llabs(cross(h[i], h[ni], h[j]));
long long nxt = llabs(cross(h[i], h[ni], h[nj]));
if (nxt > cur) j = nj;
else break;
}
long long d1 = dist2(h[i], h[j]);
if (d1 > best) { best = d1; ans = {i, j}; }
long long d2 = dist2(h[ni], h[j]);
if (d2 > best) { best = d2; ans = {ni, j}; }
}
return {h[ans.first], h[ans.second]};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
if (!(cin >> T)) return 0;
while (T--) {
int n; cin >> n;
vector<Point> pts(n);
for (int i = 0; i < n; ++i) cin >> pts[i].x >> pts[i].y;
vector<Point> hull = convexHull(pts);
auto [a, b] = diameterRotatingCalipers(hull);
cout << a.x << ' ' << a.y << ' ' << b.x << ' ' << b.y;
if (T) cout << '\n';
}
return 0;
}
|