-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathCounting Closest Pair of Points.cpp
94 lines (94 loc) · 2 KB
/
Counting Closest Pair of Points.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
int n;
struct Points
{
double x, y;
Points() {}
Points(double x, double y) : x(x), y(y) { }
bool operator<(const Points &a) const
{
return x < a.x;
}
};
bool comp1(const Points &a, const Points &b)
{
return a.x < b.x;
}
bool comp2(const Points &a, const Points &b)
{
return a.y < b.y;
}
void printPoint(Points a)
{
cout << a.x << " " << a.y << endl;
}
Points P[10005];
typedef set<Points, bool(*)(const Points&, const Points&)> setType;
typedef setType::iterator setIT;
setType s(&comp2);
double euclideanDistance(const Points &a, const Points &b)
{
// prnt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
map<double, map<double, int> > CNT;
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt","r",stdin);
while ((cin >> n) && n)
{
FOR(i, 0, n) cin >> P[i].x >> P[i].y;
sort(P, P + n, comp1);
FOR(i, 0, n)
{
// printPoint(P[i]);
s.insert(P[i]);
CNT[P[i].x][P[i].y]++;
}
// To check repeated points :/
// for(auto it: s) printPoint(it);
double ans = 10000;
int idx = 0;
FOR(j, 0, n)
{
// cout<<"Point now: "; printPoint(P[j]);
if (CNT[P[j].x][P[j].y] > 1) ans = 0;
Points it = P[j];
while (it.x - P[idx].x > ans)
{
s.erase(P[idx]);
idx++;
}
Points low = Points(it.x, it.y - ans);
Points high = Points(it.x, it.y + ans);
setIT lowest = s.lower_bound(low);
if (lowest != s.end())
{
setIT highest = s.upper_bound(high);
for (setIT now = lowest; now != highest; now++)
{
double cur = sqrt(euclideanDistance
(*now, it));
// prnt(cur);
if (cur == 0) continue;
// cout<<"Here:"<<endl;
// printPoint(*now); printPoint(it); prnt
(cur);
if (cur < ans)
{
ans = cur;
}
}
}
s.insert(it);
}
// cout<<"Set now:"<<endl;
// for(auto I: s) printPoint(I);
if (ans < 10000) cout << setprecision(4) << fixed << ans << endl;
else prnt("INFINITY");
s.clear();
CNT.clear();
}
return 0;
}