-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclosestPair.cpp
97 lines (82 loc) · 2.27 KB
/
closestPair.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
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct point {
ll x;
ll y;
};
bool compX(point a, point b) {
return a.x < b.x;
}
bool compY(point a, point b) {
if(a.y < b.y)
return true;
if(a.y > b.y)
return false;
return a.x < b.x;
}
point result1, result2;
double minDist = DBL_MAX;
double euclideanDistance(point a, point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
double bruteMin(vector<point> &pointsByX, ll low, ll high) {
ll i,j;
double dist,bestDistance = DBL_MAX;
for(i=low; i<=high; i++) {
for(j=i+1; j<=high; j++) {
dist = euclideanDistance(pointsByX[i], pointsByX[j]);
if(dist < bestDistance) {
bestDistance = dist;
if(bestDistance < minDist) {
minDist = bestDistance;
result1 = pointsByX[i];
result2 = pointsByX[j];
}
}
}
}
return bestDistance;
}
double closestPair(vector<point> &pointsByX, vector<point> &pointsByY, ll low, ll high) {
ll i,j,n = high - low + 1;
if (n <= 3) {
return bruteMin(pointsByX, low, high);
}
ll mid = low + (high - low)/2;
double distLeft = closestPair(pointsByX, pointsByY, low, mid);
double distRight = closestPair(pointsByX, pointsByY, mid+1, high);
double bestDistance = min(distLeft, distRight);
vector<point> pointsInStrip;
for(i=0; i<n; i++) {
if(abs(pointsByY[i].x - pointsByX[mid].x) < bestDistance)
pointsInStrip.push_back(pointsByY[i]);
}
for(i=0; i<pointsInStrip.size(); i++) {
for(j=i+1; j<pointsInStrip.size() && abs(pointsInStrip[i].y - pointsInStrip[j].y) < bestDistance; j++) {
double dist = euclideanDistance(pointsInStrip[i], pointsInStrip[j]);
if(dist < bestDistance) {
bestDistance = dist;
minDist = dist;
result1 = pointsInStrip[i];
result2 = pointsInStrip[j];
}
}
}
return bestDistance;
}
int main() {
ll i,n;
cin>>n;
vector<point> pointsByX(n), pointsByY(n);
for(i=0; i<n; i++) {
cin>>pointsByX[i].x>>pointsByX[i].y;
pointsByY[i].x = pointsByX[i].x;
pointsByY[i].y = pointsByX[i].y;
}
sort(pointsByX.begin(), pointsByX.end(), compX);
sort(pointsByY.begin(), pointsByY.end(), compY);
cout<<"Shortest distance = "<<closestPair(pointsByX, pointsByY, 0, n-1)<<endl;
cout<<"Points are ("<<result1.x<<","<<result1.y<<") and ("<<result2.x<<","<<result2.y<<")\n";
return 0;
}