求凸包直径 poj2187

来源:互联网 发布:iphone软件源大全 编辑:程序博客网 时间:2024/05/29 02:05
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 38702 Accepted: 11983

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

40 00 11 11 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)

Source

思路:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int N = 50005;struct Point{    int x,y;}p[N],Stack[N];int n;int mult(Point a,Point b,Point c){    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);}int dis(Point a,Point b){    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}int cmp(Point a,Point b){    if(mult(a,b,p[0])>0) return 1;    if(mult(a,b,p[0])==0&&dis(b,p[0])>dis(a,p[0])) return 1;    return 0;}int Graham(){    sort(p+1,p+n,cmp);    int top = 2;    Stack[0]=p[0];    Stack[1]=p[1];    Stack[2]=p[2];    for(int i=3;i<n;i++){        while(top>=1&&mult(p[i],Stack[top],Stack[top-1])>=0){            top--;        }        Stack[++top]=p[i];    }    return top;}int rotating_calipers(int top)//旋转卡壳{    int q=1,ans=0,p;    Stack[++top]=Stack[0]; ///将最后一个点和第一点的边也要算进去,于是多加一个点表示第一个点    for(p=0; p<top; p++)    { ///找到以Stack[p] 和 Stack[p+1]这两个点为底边的最大三角形 (叉积之积为三角形有向面积)(图1)        while(mult(Stack[p],Stack[p+1],Stack[q+1])>mult(Stack[p],Stack[p+1],Stack[q]))            q=(q+1)%top;///在底边的两个点中选择与顶点距离大的        ans=max(ans,max(dis(Stack[p],Stack[q]),dis(Stack[p+1],Stack[q+1])));    }    return ans;}int main(){    while(scanf("%d",&n)!=EOF){        for(int i=0;i<n;i++){            scanf("%d%d",&p[i].x,&p[i].y);        }        int k = 0;        for(int i=1;i<n;i++){            if(p[k].y>p[i].y||(p[k].y==p[i].y)&&(p[k].x>p[i].x)){                k=i;            }        }        swap(p[0],p[k]);        //printf("%d %d\n",p[0].x,p[0].y);        int top = Graham();        int ans =rotating_calipers(top);        printf("%d\n",ans);    }    return 0;}



原创粉丝点击