Created
April 10, 2020 04:49
-
-
Save juchiast/58bd02f436db4ab844fb9b1b6d8de44f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <algorithm> | |
using namespace std; | |
struct point { | |
double x, y; | |
}; | |
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; | |
} | |
#define MAXN 100000 | |
point a[MAXN]; | |
double mindist; // biến lưu kết quả bà i toán | |
// tÃnh khoảng cách giữa a và b rá»i update kết quả | |
void upd_ans(const point &a, const point &b) { | |
double dist = sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); | |
if (dist < mindist) mindist = dist; | |
} | |
void find(int l, int r) { | |
if (r <= l) return; | |
// Äoạn [l,r] có 2 phần tá» | |
if (r == l + 1) { | |
upd_ans(a[l], a[r]); | |
// sắp các phần tỠlại theo y | |
if (!cmp_y(a[l], a[r])) swap(a[l], a[r]); | |
return; | |
} | |
int m = (l + r) / 2; | |
double midx = a[m].x; | |
find(l, m); | |
find(m+1, r); | |
static point t[MAXN]; | |
// trá»n a[l,m] và a[m+1,r] lại, lÆ°u và o mảng tạm t | |
merge(a+l, a+m+1, a+m+1, a+r+1, t, cmp_y); | |
// copy từ t vỠlại a | |
copy(t, t+r-l+1, a+l); | |
// mảng t á» Äây lÆ°u các phần tá» thá»a |x_i - midx| < mindist, | |
// vá»i sá» lượng phần tá» là tm | |
// do Äã sort nên các phần tá» sẽ có y tÄng dần | |
int tm = 0; | |
for (int i=l; i<=r; i++) if (abs(a[i].x-midx) < mindist) { | |
for (int j=tm-1; j>=0 && t[j].y > a[i].y-mindist; j--) | |
upd_ans(a[i], t[j]); | |
t[tm++] = a[i]; | |
} | |
} | |
int main() { | |
ios::sync_with_stdio(false); cin.tie(0); | |
int n; cin >> n; | |
for (int i=0; i<n; i++) cin >> a[i].x >> a[i].y; | |
mindist = 1E20; | |
sort(a, a+n, cmp_x); | |
find(0, n-1); | |
printf("%.3lf", mindist); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment