现在的位置: 首页 > 综合 > 正文

Beauty Contest(2013.09.15)(凸包)

2018年01月12日 ⁄ 综合 ⁄ 共 2546字 ⁄ 字号 评论关闭

E. Beauty Contest

3000ms
3000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
Font Size:  
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) 

这道题其实就是一个凸包的简单应用。利用凸包找出一些点来,距离最大的两个点一定是落在凸包上的

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN=50010;
int n;
struct node
{
    int x,y;
};
node p[MAXN];
int s[MAXN],top;

int cross(node p0,node p1,node p2) //计算叉积  p0p1 X p0p2
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(node p1,node p2)  //计算 p1p2的 距离
{
    return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(node p1, node p2) //极角排序函数 , 角度相同则距离小的在前面
{
    int tmp=cross(p[0],p1,p2);
    if(tmp>0) return true;
    else if(tmp==0 && dis(p[0],p1)<dis(p[0],p2)) return true;
    else return false;
}
void init()
{
    int i,k=0;
    node p0;
    scanf("%d%d",&p[0].x,&p[0].y);
    p0.x=p[0].x;
    p0.y=p[0].y;
    for(i=1; i<n; i++)
    {
        scanf("%d%d",&p[i].x,&p[i].y);
        if( (p0.y>p[i].y) || ((p0.y==p[i].y)&&(p0.x>p[i].x)) )
        {
            p0.x=p[i].x;
            p0.y=p[i].y;
            k=i;
        }
    }
    p[k]=p[0];
    p[0]=p0;
    sort(p+1,p+n,cmp);
}

void graham()
{
    int i;
    if(n==1)
    {
        top=0;
        s[0]=0;
    }
    if(n==2)
    {
        top=1;
        s[0]=0;
        s[1]=1;
    }
    if(n>2)
    {
        for(i=0; i<=1; i++) s[i]=i;
        top=1;
        for(i=2; i<n; i++)
        {
            while(top>0 && cross(p[s[top-1]],p[s[top]],p[i])<=0)
            top--;
            top++;
            s[top]=i;
        }
    }
}
int main ()
{
    int i,j;
    cin>>n;
    init();
    graham();
    int ma=0,t;
    for (i=0; i<=top; i++)
        for (j=i+1; j<=top; j++)
        {
            t=(p[s[i]].x-p[s[j]].x)*(p[s[i]].x-p[s[j]].x)+(p[s[i]].y-p[s[j]].y)*(p[s[i]].y-p[s[j]].y);
            if (t>ma) ma=t;
        }
    cout<<ma<<endl;
    return 0;
}


抱歉!评论已关闭.