POJ 2187Beauty Contest旋转卡壳

1 篇文章 0 订阅
1 篇文章 0 订阅

题目大意:给定平面上n个点,求最远的两个点的距离的平方。
旋转卡壳裸题,第一次写旋转卡壳,好鸡冻,得到的教训是斜率一定要注意INF和-INF的区别。顺便说一下旋转卡壳的做法,我们先求出一个凸包,然后在上凸壳和下凸壳一块扫,先让上凸壳跑到下一条线,再看下凸壳是否也应该跑到下一条线(感觉表达力吃鸡),总之扫一遍就好了,然后对于每个卡到的点对求一边距离的平方,就完事了。

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define INF 1e8
#define eps 1e-8
struct point
{
    int x,y;
    point(){}
    point(double _,double __):x(_),y(__){}
    double operator ^ (point b)
    {
        if(x==b.x) return (y<b.y?INF:-INF);
        return (double)(b.y-y)/(b.x-x);
    }
    bool operator < (point b) const
    {
        if(x==b.x) return y<b.y;
        return x<b.x;
    }
    int operator & (point b)
    {
        return (b.x-x)*(b.x-x)+(b.y-y)*(b.y-y);
    }
}points[100000];
point up[100000];
point down[100000];
int top1,top2;
int tot;
void get_hull()
{
    sort(points+1,points+1+tot);
    for(int i=1;i<=tot;i++)
    {
        while(top1>=2 && (up[top1-1]^up[top1])<(up[top1]^points[i])) top1--;
        up[++top1]=points[i];
    }
    for(int i=1;i<=tot;i++)
    {
        while(top2>=2 && (down[top2-1]^down[top2])>(down[top2]^points[i])) top2--;
        down[++top2]=points[i];
    }
}
int ans=0;
void rot()
{
    for(int i=1,j=top2;i<=top1;i++)
    {
        double slope1=(i==top1 ? -INF : up[i]^up[i+1]);
        ans=max(ans,up[i]&down[j]);
        while(j>1 && (down[j-1]^down[j])>slope1)
        {
            j--;
            ans=max(ans,up[i]&down[j]);
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    tot=n;
    for(int i=1;i<=n;i++) scanf("%d%d",&points[i].x,&points[i].y);
    get_hull();
    rot();
    cout<<ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值