[Computational Geometry][Convex Hull Diameter]Beauty Contest

Description

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having 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 quite 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, 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 that is the squared distance between the pair of farms that are farthest apart from 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) 
 
Ideas: 1. First use Graham to solve the convex hull 2. Use Rotating_Caliper to find the diameter of the convex hull
AC code:
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

int n;
int top;
int ans;
struct Point{
  int x,y;
} point [ 500100 ], stk [ 500100 ];

int dis(Point a,Point b){
  return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

int cross(Point a,Point b,Point c){
  return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

void find_miny(){
   Point tmp=point[0];
   int flag=0;
   for(int i=1;i<n;i++){
     if(point[i].y<tmp.y||(point[i].y==tmp.y&&point[i].x<tmp.x)){
        tmp=point[i];
        flag=i;
     }
   }
   if(flag) swap(point[0],point[flag]);
}

bool cmp(Point a,Point b){
   int n=cross(point[ 0 ],a,b);
   if (n> 0 ||(n== 0 &&dis(point[ 0 ],a)>dis(point [ 0 ],b))) return  true ;//When point[0],a,b are collinear, the row farther from point[0] is in front
   return  false ;
}

void Graham(){
  top=-1;
  stk[++top]=point[0]; stk[++top]=point[1];
  for(int i=2;i<n;i++){
    while(top&&cross(stk[top-1],stk[top],point[i])<0) top--;
    stk [ ++ top] = point [i];
  }
}

void RC(){//rotate card (qia) shell
   years = 0.0 ;
   pcs [ ++ top] = point [ 0 ];
   int j = 0 ;
   for ( int i = 1 ; i <= top; i ++ ) {
      while (abs (cross (stk [i- 1 ], stk [i], stk [j])) <abs (cross (stk [i- 1 ] , stk [i], stk [j + 1 ]))) j = (j + 1 )% n;
     ans =max(ans,max(dis(stk[j],stk[i- 1 ]),dis(stk[j],stk[i])));
   }
}

intmain ()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&point[i].x,&point[i].y);
    }
    if(n==2) {
        printf("%d\n",dis(point[0],point[1]));
        return 0;
    }
    find_miny();
    sort(point+1,point+n,cmp);
    Graham();
    RC();
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325267363&siteId=291194637