-
Notifications
You must be signed in to change notification settings - Fork 156
/
2194 - Minimum Euclidean Distance.cpp
52 lines (47 loc) · 1.24 KB
/
2194 - Minimum Euclidean Distance.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
/*
Problem Name: Minimum Euclidean Distance
Problem Link: https://cses.fi/problemset/task/2194
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define P pair<int, int>
#define X first
#define Y second
int norm(P a, P b) {
return (b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y);
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin>>n;
vector<P> v(n);
int d = LLONG_MAX;
for (int i = 0; i < n; i++) {
int x, y; cin >> x >> y;
v[i] = {x, y};
}
sort(v.begin(), v.end());
set<P> s = {{v[0].Y, v[0].X}};
int j = 0;
for (int i = 1; i < n; i++) {
auto it = s.begin();
int dd = ceil(sqrt(d));
while (j < i && v[j].X < v[i].X - dd) {
s.erase({v[j].Y, v[j].X});
j++;
}
auto l = s.lower_bound({v[i].Y - dd, 0});
auto r = s.upper_bound({v[i].Y + dd, 0});
for (auto it = l; it != r; it++) {
d = min(d, norm({it->Y, it->X}, v[i]));
}
s.insert({v[i].Y, v[i].X});
}
cout << d;
}