-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClosetSetPair.cpp
67 lines (63 loc) · 1.4 KB
/
ClosetSetPair.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
struct point2D
{
double x, y;
bool operator< (point2D const other) const{
return x < other.x;
}
bool operator> (point2D const other) const{
return y > other.y;
}
};
point2D p[10000+10];
double dis(point2D p1, point2D p2)
{
return sqrt(((p1.x - p2.x) * (p1.x - p2.x)) + ((p1.y - p2.y) * (p1.y - p2.y)));
}
double bruteforce(int start, int n){
double mind = 2e9;
for(int i = start; i < n - 1; i++){
for(int j = i + 1; j < n; j++){
mind = min(mind, dis(p[i], p[j]));
}
}
return mind;
}
double findcp(int left, int right,int n)
{
if(n <= 3){
return bruteforce(left, n);
}
double mind;
int mid = left + (right - left) / 2;
double cl = findcp(left, mid, mid - left + 1);
double cr = findcp(mid + 1, right, right - mid);
mind = min(cl, cr);
vector<point2D> v;
for(int i = left; i <= right; i++){
if(p[i].x <= p[mid].x + mind && p[i].x >= p[mid].x - mind)
v.push_back(p[i]);
}
sort(v.begin(), v.end(), greater<point2D>());
for(vector<point2D>::iterator it = v.begin(); it != v.end()-1; it++){
for(vector<point2D>::iterator jt = it + 1; jt != v.end(); jt++){
mind = min(mind, dis(*it, *jt));
}
}
return mind;
}
int main(int argc, char const *argv[])
{
int n;
double min;
while(cin >> n && n)
{
for(int i = 0; i < n; i++){
cin >> p[i].x >> p[i].y;
}
sort(p, p + n);
min = findcp(0, n-1, n);
if(min < 10000) printf("%.4lf\n", min);
else printf("INFINITY\n");
}
return 0;
}