Find the farthest point (convex hull ),
Source of https://biancheng.love/contest/41/problem/ B /index
Find the farthest point
Description
In the TD corridor, there was a "Brave plum blossom pile", and several pillars were standing on the water. Nova thinks it is a good job and thinks it can jump between any two pillars. Now he wants to challenge the two pillars that are far away from each other. What is the maximum distance? (Since the wooden post is given in the form of horizontal and vertical coordinates, for the convenience of calculation, to avoid square root, the answer only needs to give the square of the distance)
Input
Multiple groups of test data (the number of groups cannot exceed 10), for each group of data, the first behavior is a positive integer N, representing the number of plum blossom piles, next N rows, each row two positive integers xi, yi represents the horizontal and vertical coordinates of the I-root pile. (Data is in the INT range)
Output
For each group of data, a row is output, which is the square of the distance between the two columns that are farthest away.
Input example
31 11 20 0
Output example
5
Solution:
Find the farthest point, which can be converted to the two Farthest Points on the corresponding convex hull.
Flat convex hull:
Definition: for a simple polygon, if any two points on or inside its boundary are specified, if all vertices connected to the two points are included on or inside the boundary of the polygon, the polygon is a convex polygon.
Two algorithms are introduced below to solve the problem of plane convex hull:
1. Graham scanning method. The running time is O (nlgn ).
2. The Jarvis step method runs at O (nh) and h is the number of vertices in the convex hull.
Recommendation blog:Http://blog.csdn.net/bone_ace/article/details/46239187
Recommendation blog:Http://www.cnblogs.com/jbelial/archive/2011/08/05/2128625.html
Code:
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #define INF -110 5 6 using namespace std; 7 long long i,j,k,n,top,ans; 8 9 struct Node10 {11 int x;12 int y;13 }no[1000010],stack[1000010];14 15 long long dis(Node p1,Node p2)16 {17 return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);18 }19 20 long long mult(Node p1,Node p2,Node p0)21 {22 return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y));23 }24 25 long long cmp(Node a,Node b)26 {27 if(mult(a,b,no[0])>0)28 return 1;29 else if(mult(a,b,no[0])==0&&dis(a,no[0])<dis(b,no[0]))30 return 1;31 return 0;32 }33 34 void work()35 {36 k=0;37 for(i=1; i<n; i++)38 {39 if(no[k].y>no[i].y || ((no[k].y == no[i].y) && no[k].x > no[i].x))40 k = i;41 }42 Node tmp;43 tmp = no[0];44 no[0] = no[k];45 no[k] = tmp;46 sort(no+1,no+n,cmp);47 top = 2;48 stack[0] = no[0];49 stack[1] = no[1];50 stack[2] = no[2];51 for(i=3; i<n; i++)52 {53 while(top>1 && mult(no[i],stack[top],stack[top-1])>=0)54 top--;55 stack[++top] = no[i];56 }57 }58 59 int main()60 {61 while(~scanf("%lld",&n))62 {63 for(i=0; i<n; i++)64 scanf("%lld%lld",&no[i].x,&no[i].y);65 work();66 ans=INF;67 for(i=0; i<=top; i++)68 {69 for(j=i+1; j<=top; j++)70 {71 if(ans<dis(stack[i],stack[j]))72 ans=dis(stack[i],stack[j]);73 }74 }75 printf("%lld\n",ans);76 }77 return 0;78 }