Beauty Contest(poj 2187)

来源:互联网 发布:淘宝小二介入订单关闭 编辑:程序博客网 时间:2024/06/11 17:22
Beauty Contest
Time Limit: 3000MS
Memory Limit: 65536KTotal Submissions: 35352
Accepted: 10943

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

<span style="box-sizing: border-box; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei'; border: 0px; outline: 0px; font-size: 18px; vertical-align: baseline; background: transparent;">40 00 11 11 0</span>

Sample Output

<span style="box-sizing: border-box; margin: 0px; padding: 0px; font-family: 'Microsoft YaHei'; border: 0px; outline: 0px; font-size: 18px; vertical-align: baseline; background: transparent;">2</span>

Hint

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

题意:求两点之间的最大距离,经典的凸包问题

1.根据点的位置进行排序

2.分别建立上下的凸包

3.求最长距离

若两个向量OA和OB的叉积等于xAyB-xByA.判断左右:顺着向量v看,如果w在左边,那么v和w的叉积大于0,否则小于0.(用手在坐标上画一下看看)。

#include <iostream>  #include<cstring>  #include<cstdio>  #include<algorithm>  #include<cmath>  using namespace std;  struct point{    int x,y;}p[50005],ch[50005];bool cmp(point a,point b){    if(a.x!=b.x)return a.x<b.x;    else return a.y<b.y;}int judge(point a,point b,point c){    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);}long long ass(point a,point b){    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)            scanf("%d%d",&p[i].x,&p[i].y);        sort(p,p+n,cmp);        int top=0;        for(int i=0;i<n;i++)        {            while(top>1&&judge(ch[top-2],ch[top-1],p[i])<=0)top--;            ch[top++]=p[i];        }        int k=top;          for(int i=n-2;i>=0;i--)        {            while(top>k&&judge(ch[top-2],ch[top-1],p[i])<=0)top--;            ch[top++]=p[i];        }        if(n>1)top--;        long long mx=-1,num;        for(int i=0;i<top;i++)        {          for(int j=0;j<top;j++)           {            num=ass(ch[i],ch[j]);            if(num>mx)mx=num;           }        }        printf("%lld\n",mx);    }      return 0;}



0 0
原创粉丝点击