forked from brc-dd/dsa-coursera-assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest.cpp
60 lines (51 loc) · 1.47 KB
/
closest.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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define float double
#define double long double
#undef FLT_MAX
#define FLT_MAX DBL_MAX
#undef DBL_MAX
#define DBL_MAX LDBL_MAX
#define x first
#define y second
#define mp make_pair
#define all(v) v.begin(), v.end()
#define pb push_back
#define define_and_read_points(v, n) \
Points v(n); \
for (auto &i : P) \
cin >> i.x >> i.y
typedef pair<int, int> Point;
typedef vector<Point> Points;
inline float dist(Point &p1, Point &p2) {
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
float min_distance(Points::iterator beg, Points::iterator end) {
size_t n(end - beg), mid(n / 2);
if (n == 2)
return dist(*beg, *(beg + 1));
if (n == 3)
return std::min(dist(*beg, *(beg + 1)), std::min(dist(*(beg + 1), *(beg + 2)), dist(*(beg + 2), *beg)));
auto mid_point(*(beg + mid));
auto d = std::min(min_distance(beg, beg + mid), min_distance(beg + mid, end));
Points strip;
for (auto i = beg; i != end; i++)
if (abs(i->x - mid_point.x) < d)
strip.pb(*i);
sort(all(strip), [](auto &a, auto &b) {
return a.y < b.y;
});
for (size_t i(0); i < strip.size(); i++)
for (size_t j(i + 1); j < strip.size() and (strip[j].y - strip[i].y) < d; j++)
d = std::min(d, dist(strip[i], strip[j]));
return d;
}
int32_t main() {
int n;
cin >> n;
define_and_read_points(P, n);
sort(all(P));
cout << fixed << setprecision(4) << min_distance(all(P));
return 0;
}