-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosest P_pair.cpp
More file actions
50 lines (43 loc) · 1.19 KB
/
Closest P_pair.cpp
File metadata and controls
50 lines (43 loc) · 1.19 KB
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
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mxn = 0x7fffffff;
int n;
struct Point{
double x,y;
}p[N],tmp[N];
bool cmp_x(const Point &a,const Point &b){
return a.x < b.x;
}
bool cmp_y(const Point &a, const Point &b){
return a.y < b.y;
}
double get_dis(const Point &a, const Point &b){
double dx = a.x - b.x, dy = a.y - b.y;
return sqrt(dx*dx + dy*dy);
}
double find_mindis(int l, int r){
double ret(mxn);
if(l == r) return ret;
int mid = (l+r) >> 1;
ret = min(find_mindis(l,mid),find_mindis(mid+1,r));
int cnt(0);
for(int i = mid; i >= l && p[mid].x - p[i].x < ret; i--) tmp[cnt++] = p[i];
for(int i = mid+1; i <=r && p[i].x - p[mid].x < ret; i++) tmp[cnt++] = p[i];
sort(tmp,tmp+cnt,cmp_y);
for(int i = 0; i < cnt; i++){
for(int j = i+1; j < cnt && tmp[j].y-tmp[i].y < ret; j++){
ret = min(ret,get_dis(tmp[i],tmp[j]));
}
}
return ret;
}
int main(){
while(cin >> n && n){
for(int i = 0; i < n; i++)
cin >> p[i].x >> p[i].y;
sort(p,p+n,cmp_x);
double dis = find_mindis(0,n-1)/2.0;
cout << fixed << setprecision(2) << dis << endl;
}
}