POJ2187 Beauty Contest

原题链接:http://poj.org/problem?id=2187
洛谷链接:https://www.luogu.org/problemnew/show/P1452

Beauty Contest

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

Hint

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

题目大意

求凸包直径的平方

题解

日常求凸包一波,然后使用旋转卡壳求凸包直径。  
每次以一条边为基准,用一条与之平行的线去“卡”凸包,卡到对面的点上,这样就可以找到一组点更新直径。在此基础上,我们就不断旋转这两条平行线来“卡”凸包,即可在O(n)的时间找到正解。
代码实现方面,我们只需要不停的以基准边为底求三角形面积最大就可以找到对应的点。

代码
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#define db int
using namespace std;
const int M=5e4+5;
struct pt{db x,y;};
bool operator <(pt a,pt b){return a.x==b.x?a.y<b.y:a.x<b.x;}
pt operator -(pt a,pt b){return (pt){a.x-b.x,a.y-b.y};}
db operator *(pt a,pt b){return a.x*b.y-a.y*b.x;}
db sqr(db x){return x*x;}
db dis(pt a,pt b){return sqr(a.x-b.x)+sqr(a.y-b.y);}
db area(pt a,pt b,pt c){return (b-a)*(c-a);}
int n,r,f,top;
char c;
int read()
{
    r=0;f=1;
    c=getchar();
    while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    while(isdigit(c)){r=(r<<1)+(r<<3)+c-'0';c=getchar();}
    return r*f;
}
pt p[M],sta[M];
void in()
{
    n=read();
    for(int i=1;i<=n;++i)
    p[i].x=read(),p[i].y=read();
}
void tubao()
{
    sort(p+1,p+1+n);
    for(int i=1;i<=n;++i)
    {
        while(top>1&&(sta[top]-sta[top-1])*(p[i]-sta[top-1])<=0)--top;
        sta[++top]=p[i];
    }
    int k=top;
    for(int i=n-1;i>=1;--i)
    {
        while(top>k&&(sta[top]-sta[top-1])*(p[i]-sta[top-1])<=0)--top;
        sta[++top]=p[i];
    }
}
void ac()
{
    int ans=0;
    if(top==2)printf("%d",dis(sta[1],sta[2])),exit(0);
    int j=2;
    for(int i=1;i<=top;++i)
    {
        while(area(sta[i],sta[i+1],sta[j])<area(sta[i],sta[i+1],sta[j+1]))j=(j+1)%top;
        ans=max(ans,max(dis(sta[i],sta[j]),dis(sta[i+1],sta[j])));
    }
    printf("%d",ans);
}
int main()
{
    in();tubao();
    ac();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值