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:
- 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;
- When taking the midpoint violence, remember not to compare with yourself (distance is 0);
- Unless there is a string, do not use CIN,scanf will be much faster;
- 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);
- 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