"Hdoj 1007" nearest point pair

Source: Internet
Author: User

Title: Portal

Answer: the direct brute force solution will definitely time out. The core of this problem is to find out the nearest pair of points between the distance, that is, the nearest point to the algorithm.

In brief, the idea of divide and conquer is used to compare the minimum distance of the left half with the minimum distance of the right half to get a mindist. Then traverse the boundary between the left and right mindist, and take the minimum value.

In this way, only the points around the dividing line need to be violent. But the first time I commit a version is a timeout. After the inquiry is because the optimization is not enough, write in trick.

Here are some trick:

    1. Dividing line: Not necessarily the distance on the equal, according to the position of the x-axis of the number of equal (take the most middle point ) about better;
    2. When taking the midpoint violence, remember not to compare with yourself (distance is 0);
    3. Unless there is a string, do not use CIN,scanf will be much faster;
    4. I've always wanted to throw up the groove ... Data accuracy, do OJ do not use float, directly on double(troubled me for a long time);
    5. Floating-point numbers (float,double) do not exist exactly equal (see here). EPS (typically 1e-6, 1e-8) can be used to determine whether the range is less than EPS using Fabs (ABS is the absolute value of integers).
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <vector> #include <map > #include <algorithm> #include <math.h> #include <string> #include <string.h> #define    MAXDIST 1000000using namespace std;struct point{double x; Double y;}; int n;double x, y;bool tag; Point tmp;double eps = 1e-8;    Point Gao[100005];bool cmpxy (const-point A, const-point B) {if (a.x! = b.x) {return a.x < b.x;    } else {return a.y < b.y; }}bool cmpx (const point A, const point B) {return a.x < b.x;} BOOL Cmpy (const point A, const point B) {return a.y < b.y;} Double Dist (const point A, const point B) {return ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-B.Y));}    Double Nearestpair (int head, int tail) {if (head = = tail) {return MAXDIST;    } if (head + 1 = = tail) {return dist (Gao[head], gao[tail]);    } int mid = (head + tail) >> 1; Double D1 = Nearestpair (head, MID);    Double D2 = Nearestpair (mid + 1, tail); Double mindist = d1 > d2?    D2:D1; for (int i = head; i<=tail; i++) {//cannot compare with itself, otherwise the distance is 0 if (i! = Mid && gao[i].x > (gao[mid].x                -mindist) && gao[i].x < (gao[mid].x + mindist)) {if (Dist (gao[i], Gao[mid]) < mindist)        Mindist = Dist (Gao[i], gao[mid]); }} return mindist;}        int main () {while (Cin>>n && n!= 0) {memset (GAO, 0, sizeof (GAO));        Tag = false;            for (int i=0; i<n; i++) {scanf ("%lf%lf", &x, &y);            tmp.x = x;            Tmp.y = y;        Gao[i] = tmp;        } sort (Gao, Gao + N, cmpxy); for (int i=0; i < n-1; i++) {if (Fabs (gao[i].x-gao[i+1].x) < EPS && fabs (gao[i].y-gao[i        +1].Y) < EPS) tag = true;            } if (tag) {cout<< "0.00" <<endl; ContInue;            } else {Double d = nearestpair (0, n-1);        printf ("%.2f\n", sqrt (d)/2); }} return 0;}

"Hdoj 1007" nearest point pair

Related Article

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.