2017-04-04 在校训练T3 选美比赛 contest

本文介绍了一道计算几何题目,讲述如何帮助名为贝西的母牛计算其环球旅行中可能遇到的最大农场间的距离,以便准备合适大小的手提箱携带足够的食物。文章通过叉乘和旋转卡壳算法构建凸包,并最终找到最远点对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

选美比赛 contest

Description
约翰农夫的母牛贝西,刚刚在牛选美比赛赢得第一名,赢得了“世界牛小姐”称号。为了传播善意,贝西将参观世界各地的农场N(2 < = N < = 50000)。为简单起见,世界将被表示成一个二维平面,其中每个农场位于一对整数坐标(x,y),其值在-2000000~2000000范围内。没有两个农场共享相同的坐标。
尽管贝西旅行中走农场之间的直线,但一些农场之间的距离可能很大,所以她想带着一个手提箱,足够她装食物,即使最远的距离。她想确定旅行中最大可能的距离,这样她才能决定箱子的大小。帮助贝西计算所有成对的农场间最大距离。(最远点对)

Input
第一行一个整数N
以下N行,每行两个整数,表示一个农场的坐标。

Output
一行,一个整数表示最远一对农场距离的平方。

Sample Input
4
0 0
0 1
1 1
1 0

Sample Output
2

本题是先叉乘,然后旋(xuán)转(zhuǎn)卡(qiǎ)壳(ké)。
没了。

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std ;
const int maxn=50010;

int n,top;
long long ans;
typedef struct NODE{
    long long x,y;
    NODE (){}
    NODE (long long a,long long b){
        x=a,y=b;
    }
}N;
N p[maxn],stack[maxn];
N operator -(N a,N b){
    return N (a.x-b.x,a.y-b.y);
}
long long operator * (N a,N b){
    return a.x*b.y-a.y*b.x;
}
long long sqr (long long x) {return x*x;}
long long dist (N x,N y){
    return sqr (x.x-y.x)+sqr(x.y-y.y);
}
bool operator <(N a,N b){
    long long cnt=((a-p[1])*(b-p[1]));
    return cnt>0 || (cnt==0&&dist(p[1],a)<dist (p[1],b));
}
void init (){
    scanf ("%d",&n);
    int i,k=1;
    for (i=1;i<=n;i++){
        scanf ("%lld %lld",&p[i].x,&p[i].y);
        if (p[i].x < p[k].x || (p[i].x==p[k].x&&p[i].y<p[k].y)) k=i;
    }
    swap (p[1],p[k]);
    sort (p+2,p+n+1);
}
void graham(){//构建凸包
    stack[++top]=p[1];
    stack[++top]=p[2];
    int i;
    for(i=3;i<=n;i++){
        while(top>1&&(stack[top]-stack[top-1])*(p[i]-stack[top-1])<=0) top--;
        stack[++top]=p[i];
    }
}
void xzkk(){
    stack[top+1]=stack[1];
    int now=2,i;
    for(i=1;i<=top;i++){
        while((stack[i+1]-stack[i])*(stack[now]-stack[i])<(stack[i+1]-stack[i])*(stack[now+1]-stack[i])){
            now++;
            if(now==top+1) now=1;
        }
        ans=max(ans,dist(stack[now],stack[i]));
    }
}
int main (){
    init ();
    graham ();
    xzkk ();
    cout <<ans;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值