"Algorithmic learning note" 36. Convex hull for maximum two points distance SJTU OJ 1244 Date A Live

Source: Internet
Author: User

Description

A TA has a lot of many sister paper, including five Crossing and Dongchuan Road and other men's vocational technical school. However, the distance allowed him to spend a lot of time rushing between the cities. In order to better arrange his dating plan, he wants to know the distance between the farthest two sister paper.

Input Format

The first line has an integer n, which indicates the number of sister paper.

The next n lines, two real numbers x, y per line, represent the coordinates of the sister paper (assumed in a planar Cartesian coordinate system).

For 80% of data, n<=2000

For 90% of data, n<=10000

For 100% of data, n<=100000

Output Format

Outputs a real number that represents the Euclidean distance (i.e. straight distance) between the farthest sister paper. The answer retains 4 decimal places.

Sample Input
31 10 010 1
Sample Output
10.0499

The mathematical model is to find the distance size of a point set up to two points.

The idea is to first use the Graham Scan method to set up the convex hull set.
And then there are two paths, one of which is the Cn2 to find the distance from any two points.
The other is to use the legendary rotation jam method to beg. Complexity is on, the basic idea is to rotate the search and convex side of the farthest point, due to convex hull convex nature, resulting in the direction of the furthest point of rotation and the direction of rotation of the edge is consistent, so compression to on

The following code is a convex hull +cn2 method implemented with an array,
Array implementations are much faster than STL vectors, and are easier to read.
A few notable points are
1. Selection of the base point, select the bottom left point, or choose the lowest-left
2. The method of comparison, cosine theorem or outer product judgment?
3. Stack top pointer vertical and stack length relationship ... = =


#include <iostream>#include<cmath>#include<algorithm>#include<cstdio>using namespacestd;structpoint{Doublex, y;}; Point pointset[100005];intconvexhull[100005];//convex hull as long as the ID of the storage pointintps_len=0;//the size of the point setintch_len=0;//stack pointers for convex packagesostream&operator<< (Ostream & out,Constpoint&p) { out<<'('<<p.x<<','<<p.y<<')'<<Endl; return  out;}//calculates the negative representation of an outer product clockwise to indicate that 0 is a collinearInlineDoubleCrossproduct (ConstPoint &p1,ConstPoint &AMP;P2,ConstPoint &p0) {    return(p1.x-p0.x) * (P2.Y-P0.Y)-(P1.Y-P0.Y) * (p2.x-p0.x);}//returning a positive number indicates that you want to swap two numbers and put B in front of a counterclockwise orderintCmptwopoints (Const void*a,Const void*b) { Point* P1 = (point*) A; Point* P2 = (point*) b; //Note: The base point is already pointset[0]//The first method uses the cosine theorem to judge//The second method uses cross-multiplication to indicate that if < P0,P1, p0,p2 > is negative, it means pointing to the inside of the paper and clockwise    Doubleres = crossproduct (*P1, *P2, pointset[0]); if(res<0)        return 1; if(res = =0and P2->x < p1->x)//if collinear and P2 closer        return 1; return-1;}//inline point CH (int CHid)// {//return Pointset[convexhull[chid]];// }//set up a convex bagvoidBuildconvexhull () {//Step1 Find the bottom-most-left point of the Datum point    intBasepoint=0;  for(inti =1; i < Ps_len; ++i) {//if (Pointset[i].y < Pointset[basepoint].y or (POINTSET[I].Y==POINTSET[BASEPOINT].Y and pointset[i].x< pointset[basepoint].x))        if(Pointset[i].x < pointset[basepoint].x or (pointset[i].x==pointset[basepoint].x and pointset[i].y<pointset[basepoint].y)) Basepoint=i; } Swap (pointset[basepoint],pointset[0]);//change the base point to the first position of PS//Step2 sort the points in the Pointset according to the size of their connection to the base point and the angle of the x axis .Qsort (pointset+1, ps_len-1,sizeof(point), cmptwopoints);//sort without a base point//Printps (); //Step3 Start build convex packageconvexhull[0] =0;//the base point must be the first dot on the convex hull into the stackconvexhull[1] =1;//preset The first non-base point is also the point on the convex packet stackconvexhull[2] =2; Ch_len=2;//pointer to top of stack element//start DFS --stacks         for(inti =3; i < Ps_len; ++i)//study all the remaining points    {         //If you let it into the stack it can and the previous point before a point is counterclockwise rotation, then temporarily can join or kick out x points until you can continue         while(Ch_len >0and Crossproduct (pointset[convexhull[ch_len]],pointset[i],pointset[convexhull[ch_len-1]]) <0) Ch_len--;//abandon this concave point.Convexhull[++ch_len] = i;//temporarily let this point into the stack}}inlineDouble_dis (Point p1, point p2) {returnsqrt (POW (p1.x-p2.x),2) +pow (P1.Y-P2.Y),2));} InlineDouble_max (DoubleADoubleb) {    returnA>b?a:b;}DoubleCal () {Doubleres =0;  for(inti =0; I < ch_len+1-1; ++i) for(intj = i+1; J < ch_len+1; ++j) Res=_max (Res,_dis (Pointset[convexhull[i]],pointset[convexhull[j])); returnRes;}intMainintargcChar Const*argv[]) {cin>>Ps_len;  for(inti =0; i < Ps_len; ++i) scanf ("%LF%LF",&pointset[i].x,&pointset[i].y);    Buildconvexhull (); printf ("%.4LF", Cal ()); return 0;}
View Code

Several related blogs:

Http://www.cnblogs.com/devymex/archive/2010/08/09/1795392.html

http://blog.csdn.net/hackbuteer1/article/details/7484746






"Algorithmic learning note" 36. Convex hull for maximum two points distance SJTU OJ 1244 Date A Live

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.