-
Notifications
You must be signed in to change notification settings - Fork 23
/
cmap.cpp
102 lines (82 loc) · 2.47 KB
/
cmap.cpp
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
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
};
istream& operator>>(istream& is, Point& pt) {
return is >> pt.x >> pt.y;
}
ostream& operator<<(ostream& os, const Point& pt) {
return os << "(" << pt.x << ", " << pt.y << ")";
}
double distance_square(Point a, Point b) {
double dx = double(a.x) - double(b.x),
dy = double(a.y) - double(b.y);
return dx * dx + dy * dy;
}
double min_distance(size_t num_pts, const Point* xs, const Point* ys) {
if (num_pts <= 1) {
return INFINITY;
}
if (num_pts == 2) {
return distance_square(xs[0], xs[1]);
}
if (num_pts == 3) {
double a = distance_square(xs[0], xs[1]),
b = distance_square(xs[0], xs[2]),
c = distance_square(xs[1], xs[2]);
return min(a, min(b, c));
}
int mid = num_pts / 2;
int mid_x = xs[mid].x;
const Point* left_xs = xs;
const Point* right_xs = xs + mid;
vector<Point> left_ys, right_ys;
left_ys.reserve(mid);
right_ys.reserve(mid);
for (size_t i = 0; i < num_pts; ++i) {
if (ys[i].x < mid_x) {
left_ys.push_back(ys[i]);
} else {
right_ys.push_back(ys[i]);
}
}
double min_left = min_distance(mid, left_xs, left_ys.data()),
min_right = min_distance(mid, right_xs, right_ys.data());
double d = min(min_left, min_right);
vector<Point> closer;
closer.reserve(num_pts);
for (size_t i = 0; i < num_pts; ++i) {
double dx = abs(ys[i].x - mid_x);
if (dx <= d) {
closer.push_back(ys[i]);
}
}
if (closer.size() > 0) {
size_t i = 0;
while (i < closer.size() - 1) {
size_t j = i + 1;
while (j < closer.size() && (j - i) <= 8) {
d = min(d, distance_square(closer[i], closer[j]));
++j;
}
++i;
}
}
return d;
}
int main() {
ifstream in("cmap.in");
size_t num_pts;
in >> num_pts;
vector<Point> pts(num_pts);
for (size_t i = 0; i < num_pts; ++i) {
in >> pts[i];
}
vector<Point> xs = pts, ys = pts;
sort(xs.begin(), xs.end(), [] (const Point& a, const Point& b) { return a.x < b.x; });
sort(ys.begin(), ys.end(), [] (const Point& a, const Point& b) { return a.y < b.y; });
ofstream out("cmap.out");
double d_squared = min_distance(num_pts, xs.data(), ys.data());
out << fixed << setprecision(6) << sqrt(d_squared) << '\n';
}