Ultraviolet A 10245-The closest pair Problem

Source: Internet
Author: User

[LINK]

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 1186

[Original question]

Given a set of points in a two dimenstmspace, you will have to find the distance between the closest two points.

 

Input

 

The input file contains several sets of input. Each set of input starts with an integerN (0 <= n <= 10000), Which denotes the number of points in this set. The nextNLine contains the coordinatesNTwo-dimensional
Points. The first of the two numbers denotesX-CoordinateAnd the latter denotesY-Coordinate. The input is terminated by a set whoseN = 0. This set shoshould not be processed. The value of the coordinates
Will be less40000And non-negative.

 

Output

 

For each set of input produce a single line of output containing a floating point number (with four digits after the decimal point) which denotes the distance between the closest two points. if there is no such two points in the input whose distance is less
Than10000, Print the lineInfinity.

 

Sample Input

3
0 0
10000 10000
20000 20000
5
0 2
6 67
43 71
39 107
189 140
0

Sample output

Infinity
36.2215

[Topic]

Given the coordinates of the points on N planes, it is required to output the shortest distance between these points. If the shortest distance exceeds 10000, save the output infinity.

[Analysis and Summary]

In computational ry, the question of "finding the nearest point" is described in Chapter 33.4 of Introduction to algorithms. This algorithm uses the idea of division and the complexity is O (n lg n ). However, implementation is quite troublesome.

Here the method is O (n lg n), which simplifies one of the steps, that is, the following sort, so that the complexity of this step is changed from O (N) to O (n lg n ).



[Code]
/** Ultraviolet A: 10245-The closest pair problem * Time: 0.080 * Author: d_double **/# include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; const double INF = 1e20; const int n = 100005; struct point {Double X; Double Y; friend bool operator <(const point & A, const point & B) {if (. x! = B. x) return. x <B. x; return. Y <B. Y ;}} point [N]; int N; int tmpt [N]; bool cmpy (const Int & A, const Int & B) {return point [A]. Y <point [B]. y;} double min (double A, double B) {return a <B? A: B;} double DIS (int I, Int J) {return SQRT (point [I]. x-point [J]. x) * (point [I]. x-point [J]. x) + (point [I]. y-point [J]. y) * (point [I]. y-point [J]. y);} double closest_pair (INT left, int right) {double D = inf; If (Left = right) return D; If (left + 1 = right) return DIS (left, right); int mid = (left + right)> 1; double d1 = closest_pair (left, mid); double D2 = closest_pair (Mid + 1, right); D = min (D1, D2); int I, J, k = 0; // separate the range for (I = left; I <= right; I ++) {If (FABS (point [Mid]. x-point [I]. x) <= d) tmpt [k ++] = I;} Sort (tmpt, tmpt + k, cmpy); // linear scan for (I = 0; I <K; I ++) {for (j = I + 1; j <K & J <= K + 7 & point [tmpt [J]. y-point [tmpt [I]. Y <D; j ++) {double D3 = DIS (tmpt [I], tmpt [J]); If (D> D3) d = D3 ;}} return D;} int main () {freopen ("input.txt", "r", stdin); While (scanf ("% d", & N )! = EOF) {If (n = 0) break; For (INT I = 0; I <n; I ++) scanf ("% lf ", & point [I]. x, & point [I]. y); sort (point, point + n); double ans = closest_pair (0, n-1); If (ANS <10000) printf ("%. 4lf \ n ", ANS); else printf (" infinity \ n ");} return 0 ;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)


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.