2016-level algorithm sixth time on machine-e.bamboo eat me a punch

Source: Internet
Author: User

Bamboo's Eating Me punch analysis

When the distance between the two points is <=d, the fist can be punched, so that the point to meet the punch condition is the least, but not 0.

Finding the nearest point-to-distance, the nearest distance can make the number of punches possible, because other combinations are not eligible except for the nearest point.

The minimum distance to find two points in a pile of points is that the violent O (n^2) calculation is horrible, and the problem can be made smaller by splitting the mind:
Divide the points on the plane into two dials, the nearest two points may only appear in: The first pile, the second pile, and the two piles 2 each one point
Decomposition
Imagine a vertical line dividing the given point set into two dials: all points are either to the left of the line or to the right. In ascending order by x-coordinate.
Solve
Divide two recursive calls, find the nearest pair in the left, and find the nearest pair in the right one at a time. Take d ' as the minimum value in both
Merge
The nearest point-to-distance d is either a recursive-found d ', or a point pair with a point on the left-hand side. It is necessary to find out if there is a distance less than d ' and one on the left at the right point. If the distance between these two points is <d ', then the two points are actually within the distance of the vertical line d '. That is, the vertical line for the axis, width of 2*d ' range, the point in this range is taken out, and in accordance with the ascending order of Y, only need to go through a bit with the range around 7 points within the distance can be, compared to choose a smaller value.

Code
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <iomanip>using namespaceStdConst intMaxx =1e5+3;intNum[maxx];structpoint{intx, y;} P[maxx];BOOLCMPX (ConstPoint&a,Constpoint& b)//sorted by x-coordinate{returna.x<b.x;}BOOLCmpy (Const int& A,Const int&AMP;B)//Sort by y-coordinate{returnP[A].Y&LT;P[B].Y;}DoubleDis (point A, point B)//Calculate two points distance{return((Double) (a.x-b.x) * (a.x-b.x) + (Double) (A.Y-B.Y) * (A.Y-B.Y));}DoubleClosestintLowintHigh//Find the nearest distance{if(Low +1= = high)//Two point situation, already recursive to the end        returnDis (P[low], P[high]);if(Low +2= = high)//Three points        returnMin (Dis (p[low], P[high]), min (Dis (p[low), P[low +1]), DIS (P[low +1], P[high]));intMid = (low + high)/2;Doubleans = min (Closest (Low, mid), closest (Mid +1, high));//Two groups recursively, go to the bottom    intI, j, cnt =0; for(i = low, I <= high; i++) {if(p[i].x >= p[mid].x-ans && p[i].x <= p[mid].x + ans) num[cnt++] = i; } sort (num, num + cnt, cmpy);//This is ordered by the y-coordinate within the straight-line ans distance     for(i =0; I < CNT; i++) {intK = i +7> CNT? Cnt:i +7; for(j = i +1; J < K; J + +) {if(P[num[j]].y-p[num[i]].y > Ans) Break;        ans = min (dis (p[num[i]], p[num[j]), ans); }    }returnAns;}intMain () {intN while(~SCANF ("%d", &n)) { for(inti =0; i<n; i++) scanf ("%d %d", &p[i].x, &AMP;P[I].Y); Sort (p, p + N, cmpx);DoubleAns = closest (0N1);        Ans = sqrt (ans); printf"%.2lf\n", ans); }}
Other than that

Strictly speaking the data is to chuck the O (n^2) violence algorithm, but your grh with sin and cos to calculate the distance, the operation time is even faster than O (NLOGN) division,,,

2016-level algorithm sixth time on machine-e.bamboo eat me a punch

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.