Beauty Contest
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total submissions: 35396 |
|
accepted: 10961 |
Description Bessie, farmer John ' s prize cow, has just won a-place in a bovine beauty contest, earning the title ' Miss Cow World '. As a result, Bessie'll make a tour of n (2 <= n <= 50,000) farms around the world's order to spread goodwill Een farmers and their cows. For simplicity, the world would be represented as a two-dimensional plane, where each farm was located at a pair of integer Coordinates (X,Y), each has a value in the range-10,000 ... 10,000. No two farms share the same pair of coordinates.
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quit e large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits and she wants to determine the maximum possible distance she might Need to travel so she knows the size of suitcase she must bring. Help Bessie by computing the maximum distance among all pairs of farms.
Input * Line 1: A single integer, N
* Lines 2..n+1:two space-separated integers x and y specifying coordinate of each farm
Output * Line 1: A single integer This is the squared distance between the pair of farms that are farthest apart to each Other.
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint Farm 1 (0, 0) and Farm 3 (1, 1) have the longest distance (square root of 2)
to give some points, to find the furthest point of the plane to the square of the distance.
Thought: We know that the farthest point in the plane must be in the convex package that surrounds these points, so, we first use the Graham ' s scan algorithm to find all the points in the convex package, and then we can use the rotation of O (n) to jam to solve the remaining problems (of course, the violent enumeration is OK).
specific convex package algorithm can find my other articles oh.
AC Code:
#include <iostream> #include <algorithm> #include <cmath> #include <stdio.h> using namespace std
;
#define MAX (A,B) (a>b?a:b) #define PI acos ( -1) struct point {double x;
Double y;
Double distance (const point &b) const//Calculate distance {return (x-b.x) * (x-b.x) + (Y-B.Y) * (Y-B.Y);
Point operator-{return {X-S.X,Y-S.Y};
}
} ;
Point a[55000],data[55000]; int Crossleft (point p0,point p1,point p2)//interpretation left or right turn {return (p1.x-p0.x) * (P2.Y-P0.Y)-(P1.Y-P0.Y) * (p2.x-p0.x);} Boo
L CMP (Point p1,point P2)//Polar angle sequence {int tmp=crossleft (A[0],P1,P2);
if (tmp>0) return true;
else if (Tmp==0&&a[0].distance (p1) <a[0].distance (p2)) return true;
else return false;
BOOL Cmp_x (Point p1,point p2)//coordinate sequence {if (p1.x!=p2.x) return p1.x<p2.x;
Return p1.y<p2.y;
} void init (int n) {int i,k=0;
Point P;
scanf ("%lf%lf", &a[0].x,&a[0].y);
P=A[0]; for (I=1; i<n; i++) {scanf ("%lf%lf", &a[i].x,&a[i].y); if ((P.Y>A[I].Y) | |
((P.Y==A[I].Y) && (p.x>a[i].x))
{P=a[i];
K=i;
}} A[k]=a[0];
A[0]=p;
Sort (a+1,a+n,cmp);
int main () {int N;
while (~SCANF ("%d", &n)) {init (N); for (int i=0; i<2; i++) data[i]=a[i];
The first two points into the stack int top=1; for (int i=2; i<n; i++) {data[++top]=a[i];
New point into Stack while (Top>1&&crossleft (Data[top-2],data[top-1],data[top]) <=0)//Backward traversal to determine if there is a concave area data[top-1]=data[top],top--;
exist, out stack a point} ++top; /* Rotating Jam section */if (top==2) printf ("%.0f\n", Data[0].distance (data[1));
Case of convex hull degeneration else {double maxx=0.0; int i=0,j=0;
Pair of heel points in a direction for (int k=0; k<top; k++) { if (!cmp_x (Data[i],data[k])) i=k;
if (cmp_x (Data[j],data[k])) j=k;
int si=i,sj=j; while (i!=sj| |
J!=SI)//Rotary 180°{Maxx=max (Maxx,data[i].distance (data[j));
if (Crossleft ({0,0},data[(i+1)%top]-data[i],data[(j+1)%top]-data[j]) <0) i= (i+1)%top;
else j= (j+1)%top;
printf ("%.0f\n", Maxx); }/* End */}}