-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCLOPPAIR.cpp
More file actions
47 lines (47 loc) · 1.41 KB
/
Copy pathCLOPPAIR.cpp
File metadata and controls
47 lines (47 loc) · 1.41 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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/CLOPPAIR/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <set>
#include <vector>
#define MP make_pair
#define X second.first
#define Y first
#define ID second.second
using namespace std;
typedef long long ll;
typedef pair<ll, pair<ll, ll> > point;
int comp(point A, point B) { return A.X < B.X; }
int main() {
vector<point> P;
set<point> C;
ll n;
scanf("%lld", &n);
for (ll i = 0; i < n; i++) {
ll x1, y1;
scanf("%lld %lld", &x1, &y1);
P.push_back(MP(y1, MP(x1, i)));
}
sort(P.begin(), P.end(), comp);
double best = hypot(P[0].X - P[1].X, P[0].Y - P[1].Y);
ll melhor1 = P[0].ID, melhor2 = P[1].ID, left = 0;
C.insert(P[0]);
for (int i = 1; i < n; i++) {
while (left < i && P[i].X - P[left].X > best) C.erase(P[left++]);
for (set<point>::iterator it =
C.lower_bound(MP(P[i].Y - best, MP(P[i].X - best, 0)));
it != C.end() && P[i].Y + best >= (*it).Y; it++) {
double tentativa = hypot(P[i].X - (*it).X, P[i].Y - (*it).Y);
if (tentativa < best) {
best = tentativa;
melhor1 = P[i].ID;
melhor2 = (*it).ID;
}
}
C.insert(P[i]);
}
if (melhor1 > melhor2) swap(melhor1, melhor2);
printf("%lld %lld %.6lf\n", melhor1, melhor2, best);
return 0;
}