-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclosest_pair.cpp
More file actions
33 lines (25 loc) · 781 Bytes
/
closest_pair.cpp
File metadata and controls
33 lines (25 loc) · 781 Bytes
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
const ll MAXN = 1e5 + 10;
const ll INF = 1e9;
pair<ll,ll> A[MAXN];
bool comp(pair<ll,ll> a,pair<ll,ll> b){
return a.second < b.second;
}
ll fnc(ll L,ll R){
if(L == R) return INF;
ll M = (L+R)>>1;
ll d = min(fnc(L,M),fnc(M+1,R));
sort(A+L,A+R+1);
ll midx = A[(L+R)>>1].first;
vector<pair<ll,ll>> v;
FOR(i,L,R){
if((midx - A[i].first)*(midx - A[i].first) < d) v.push_back(A[i]);
}
sort(all(v),comp);
for(ll i=0;i<v.size();i++){
for(ll j=i+1;j<v.size();j++){
if((v[i].second - v[j].second)*(v[i].second - v[j].second) >= d) break;
d = min(d,(v[i].first - v[j].first)*(v[i].first - v[j].first) + (v[i].second - v[j].second)*(v[i].second - v[j].second));
}
}
return d;
}