HDU 1007 closest point

Source: Internet
Author: User

Divide and conquer law find the nearest point

Recursively divides points into groups to calculate the shortest distance. At this time, the shortest distance is only one of the two parts (the split point here is the mid point ).

You also need to consider the situation where two parts belong. At this time, the range of the selected point is reduced to mid-centered. Take the value 2 * mindist from the X axis of the mid point into consideration. In these points, take the mid point and leave those

The distance of Y is not greater than that of the middist point for distance calculation.


PS: At the beginning, the min function was written incorrectly and written as Max. Using Max for a long time leads to more and more points to be selected. It's not surprising.

For the use of recursive methods to achieve sub-governance, actually using a small range of results will narrow down the overall search space. Recursive functions must have range parameters.

The intuitive understanding of recursion can be the result returned after a simple case such as 1 2 3 is completed, and the group is spelled out as a global solution.

# Include <iostream> # include <math. h> # include <stdio. h> # include <stdlib. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 100005; int N; struct P {Double X, Y;} p [maxn], ANS [maxn]; double min (double A, double B) {return a <B? A: B;} bool cmpx (struct p a, struct p B) {return. x <B. x;} bool cmpy (struct p a, struct p B) {return. Y <B. y;} double dist (struct p a, struct p B) {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} double solve (int l, int R) {If (L + 1 = r) Return (Dist (P [L], p [R]); else if (L + 2 = r) return min (Dist (P [L], p [L + 1]), min (Dist (P [L + 1], P [R]), dist (P [R], p [l]); int mid = (L + r)> 1; double res = min (solve (L, mid), solve (Mid + 1, R); // int CNT = 0, I, j; for (I = L; I <= r; I ++) {If (FABS (P [I]. x-P [Mid]. x) <= res) ans [CNT ++] = P [I];} Sort (ANS, ANS + CNT, cmpy); for (I = 0; I <CNT; I ++) for (j = I + 1; j <CNT; j ++) {If (ANS [J]. y-ans [I]. y> = res) break; Res = min (Res, dist (ANS [I], ANS [J]);} return res;} int main () {While (scanf ("% d", & N), n) {int I; for (I = 0; I <n; I ++) scanf ("% lf", & P [I]. x, & P [I]. y); // input vertex sort (p, p + N, cmpx); // sort by X double res = solve (0, n-1); printf ("%. 2lf \ n ", Res/2);} return 0 ;}


HDU 1007 closest point

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.