-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosest pair of points.cpp
89 lines (68 loc) · 1.84 KB
/
Closest pair of points.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
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
using ci = const int;
using ld = long double;
using llint = long long;
using ullint = unsigned long long;
using pii = pair <int,int>;
using pcc = pair <char,char>;
using pss = pair <string,string>;
using vi = vector <int>;
using vb = vector <bool>;
using vii = vi::iterator;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define all(c) c.begin(), c.end()
#define ms(name,val) memset(name, val, sizeof name)
#define np nullptr
struct Point
{
int x, y;
inline bool operator()(const Point &a, const Point &b) const
{
return (a.x < b.x);
}
};
double dist(Point a, Point &b)
{
return sqrt((a.x - b.x) * (a.x - b.x) +
(a.y - b.y) * (a.y - b.y));
}
int main()
{
ios_base::sync_with_stdio(0);
//cin.tie(0);
int n;
cin >> n;
vector <Point> p(n);
for (int t1 = 0; t1 < n; ++t1)
cin >> p[t1].x >> p[t1].y;
// sort by ascending x
sort(all(p), Point());
int left = 0; // for sweep line
double ans = INF;
set <Point, Point> S;
S.insert(p[0]);
for (int t1 = 1; t1 < n; ++t1)
{
// if point at index left + current min distance
// doesn't reach the current point, then delete it
while (left < t1 && (p[left].x + ans) < p[t1].x)
S.erase(p[left++]);
set <Point,Point>::iterator it;
it = S.lower_bound(Point{p[t1].x - ans, p[t1].y - ans});
// find all points which can be reached from the
// current point within the minimal current distance
// and try to update the answer
for (; it != S.end(); ++it)
ans = min(ans, dist(*it, p[t1]));
// add current point to the set
S.insert(p[t1]);
}
cout << ans << '\n';
return 0;
}