POJ2187 Beauty Contest 旋转卡壳

先求凸包,然后旋转卡壳输出最远点对的距离的平方
Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 24459 Accepted: 7467

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



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>

using namespace std;

const int maxn=100;
const double eps=1e-8;
const double pi=acos(-1.0);

inline double sqr(double x)
{
    return x*x;
}

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int sig(double x)
{
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}

struct point
{
    double x,y;
    point(){};
    point(double a,double b):x(a),y(b){}
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
    friend point operator + (const point &a,const point &b)
    {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator - (const point &a,const point &b)
    {
        return point(a.x-b.x,a.y-b.y);
    }
    friend bool operator == (const point &a,const point &b)
    {
        return sig(a.x-b.x)==0 && sig(a.y-b.y)==0;
    }
    friend point operator * (const point &a,const double &b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (const double &a,const point &b)
    {
        return point(a*b.x,a*b.y);
    }
    friend point operator / (const point &a,const double &b)
    {
        return point(a.x/b,a.y/b);
    }
    double norm()
    {
        return sqrt(sqr(x)+sqr(y));
    }
};

double det(const point &a,const point &b)
{
    return a.x*b.y-a.y*b.x;
}

double dot(const point &a,const point &b)
{
    return a.x*b.x+a.y*b.y;
}

double dist(const point &a,const point &b)
{
    return sqr(a.x-b.x)+sqr(a.y-b.y);
}

struct polygon_convex
{
    vector<point> p;
    polygon_convex(int Size=0){ p.resize(Size);}
    polygon_convex operator = (polygon_convex x)
    {
        p.clear();
        for(int i=0;i<x.p.size();i++)
            p.push_back(x.p[i]);
        return *this;
    }
};

bool cmp_less(const point &a,const point &b)
{
    return sig(a.x-b.x)<0||sig(a.x-b.x)==0&&sig(a.y-b.y)<0;
}

polygon_convex convex_hull(vector<point> a)
{
    polygon_convex res(2*a.size()+5);
    sort(a.begin(),a.end(),cmp_less);
    a.erase(unique(a.begin(),a.end()),a.end());
    int m=0;
    for(int i=0;i<a.size();i++)
    {
        while(m>1 && sig(det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0)
            m--;
        res.p[m++]=a[i];
    }
    int k=m;
    for(int i=int(a.size())-2;i>=0;i--)
    {
        while(m>k&&sig(det(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0)
            m--;
        res.p[m++]=a[i];
    }
    res.p.resize(m);
    if(a.size()>1) res.p.resize(m-1);
    return res;
}

double convex_diameter(polygon_convex &a,int &first,int &second)
{
    vector<point> &p=a.p;
    int n=p.size();
    double maxd=0.0;
    if(n==1)
    {
        first=second=0;
        return maxd;
    }
    #define next(i) ((i+1)%n)
    for(int i=0,j=1;i<n;i++)
    {
        while(sig(det(p[next(i)]-p[i],p[j]-p[i])-
                  det(p[next(i)]-p[i],p[next(j)]-p[i]))<0)
                    j=next(j);
        double d=dist(p[i],p[j]);
        if(d>maxd)
        {
            maxd=d;
            first=i,second=j;
        }
        d=dist(p[next(i)],p[next(j)]);
        if(d>maxd)
        {
            maxd=d;
            first=i,second=j;
        }
    }
    return maxd;
}

point p;
vector<point> a;
polygon_convex con;

int main()
{
    int n,s,e;
    while(cin>>n)
    {
        a.clear();
        for(int i=0;i<n;i++)
        {
            p.input();
            a.push_back(p);
        }
        con=convex_hull(a);
        cout<<(int)convex_diameter(con,s,e)<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值