Test Instructions:Find the distance between the closest two points in N points on a plane.
Ideas:The closest point to the problem, the use of division and treatment, first sorted by X, divided, and the points in each interval in accordance with the Y order, you can prove that the number of comparisons in each interval is constant, the complexity of O (Nlogn).
Code:
#include <bits/stdc++.h> using namespace std;
const int MAXN = 1e5 + 10; struct Node {double x, y;}
PX[MAXN], PY[MAXN];
BOOL Cmpx (const Node A, const node B) {return a.x < b.x;}
BOOL Cmpy (const Node A, const node B) {return a.y < b.y;}
Double Dis (Node A, Node B) {return sqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));}
Double Closet (int l, int r) {if (l + 1 = = r) return dis (px[l], px[r]);
if (l + 2 = = r) return min (Dis (px[l], px[r]), min (Dis (px[l + 1], px[r]), DIS (px[l], px[l + 1]));
int m = (L + r) >> 1;
Double ans = min (Closet (L, M), closet (M + 1, R));
int num = 0;
for (int i = l; I <= R; i++) if (px[i].x >= px[m].x-ans && px[i].x <= px[m].x + ans)
Py[++num] = Px[i];
Sort (py + 1, py + 1 + num, cmpy); for (int i = 1; I <= num, i++) {for (int j = i + 1; j <= Num; j + +) {if (Py[j].y-py[i].y > = ans) break; This step is written in this way, but can be proven up to no more than 6 ans = min (ans, dis (py[i], py[j]));
}} return ans;
} int main () {//freopen ("In.txt", "R", stdin);
int n; while (scanf ("%d", &n), N) {for (int i = 1; I <= n; i++) scanf ("%lf%lf", &px[i].x, &px[
I].Y);
Sort (px + 1, px + 1 + N, cmpx);
printf ("%.2f\n", closet (1, N)/2);
} return 0; }