Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define min(a, b) ((a)<(b)?(a):(b))
- #define MAX 9999.99999
- struct Point { double x, y; };
- Point p[10001];
- double dist(Point p1, Point p2) {
- return hypot(p1.x - p2.x, p1.y - p2.y);
- }
- int compare(const void *a, const void *b){
- Point *sp1 = (Point *)a;
- Point *sp2 = (Point *)b;
- if (sp1->x > sp2->x) return 1;
- else if (sp1->x < sp2->x) return -1;
- else if (sp1->y > sp2->y) return 1;
- return -1;
- }
- double closest(long a, long b) {
- double d1, d2, d, xp;
- long i, j, k;
- if(a == b)
- return MAX + 1.0;
- else if(b - a == 1)
- return dist(p[b], p[a]);
- else {
- int mid = (a + b) >> 1;
- d1 = closest(a, mid);
- d2 = closest(mid + 1, b);
- d = min(d1, d2);
- xp = p[mid].x;
- j = mid;
- do {
- k = mid + 1;
- while(xp - p[k].x < d && k <= b) {
- d1 = dist(p[k], p[j]);
- d = min(d, d1);
- k++;
- }
- j--;
- } while (xp - p[j].x < d && j >= a);
- return d;
- }
- }
- int main() {
- int t;
- double d;
- while (scanf("%d", &t), t != 0) {
- for (int i = 0; i < t; i++)
- scanf("%lf%lf", &p[i].x, &p[i].y);
- qsort(p, t, sizeof(p[0]), compare);
- d = closest(0, t - 1);
- (d > MAX) ? puts("INFINITY") : printf("%.4lf\n", d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement