HDU-1007 Division (closest point to template)

Source: Internet
Author: User
Tags min
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; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.