Beauty Contest POJ - 2187

essie, 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 Input4 0 0 0 1 1 1 1 0 Sample Output2 Hint

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


#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int mt = 50000+5000;

struct vec

{

    long long x,y;

}ovo[mt],qaq[mt];

int top;

inline long long readll()

{

    long long num=0,flag=1;

    char ch;

    do{

        ch=getchar();

        if(ch=='-') flag=-1;

    }while(ch<'0'||ch>'9');

    do{

        num=num*10+ch-'0';

        ch=getchar();

    }while(ch>='0'&&ch<='9');

    return num*flag;

}

vec operator - (vec a,vec b)

{

   vec c;c.x=a.x-b.x;c.y=a.y-b.y;

   return c;

}

long long operator * (vec a,vec b)

{

    return a.x*b.y-a.y*b.x;

}

long long dis(vec a,vec b)

{

    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);

}

bool cmp(vec a,vec b)

{

    long long t=(a-ovo[1])*(b-ovo[1]);

    if(t==0) return dis(a,ovo[1])<dis(b,ovo[1]);

    return t>0;

}

void graham(int n)

{

    int tmp=1;top=0;

    for(int i=2;i<=n;i++)

        if(ovo[i].x<ovo[tmp].x||(ovo[i]).x==ovo[tmp].x&&ovo[i].y<ovo[i].y) tmp=i;

    swap(ovo[tmp],ovo[1]);

    sort(ovo+2,ovo+n+1,cmp);

    qaq[++top]=ovo[1],qaq[++top]=ovo[2];

    for(int i=3;i<=n;i++)

      {while((qaq[top]-qaq[top-1])*(ovo[i]-qaq[top-1])<0) top--;

      qaq[++top]=ovo[i];}

     // qaq[top+1]=ovo[1];

}

int main()

{

    int n;

    while(~scanf("%d",&n))

    {

    for(int i=1;i<=n;i++) ovo[i].x=readll(),ovo[i].y=readll();

    graham(n);

    long long ans=-1;

    for(int i=1;i<=top;i++)

        for(int j=i+1;j<=top;j++)

            ans=max(ans,(long long)dis(qaq[i],qaq[j]));

    printf("%lld\n",ans);

    }

    return 0;

}

/*

4

0 0

0 1

1 1

1 0

2

*/


评论(1)

热度(6)