旋转卡壳算法学习笔记

· · 个人记录

用于求凸包的最大直径。

#include <bits/stdc++.h>
using namespace std;

inline int read()
{
    int x=0;int f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int MAXN = 2005;
struct Point{double x,y;}p[MAXN],s[MAXN];
int n,top;

inline double dis(Point a,Point b)
    {return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}

Point operator - (Point a,Point b)
{
    Point t; t.x=a.x-b.x;
    t.y=a.y-b.y; return t;
}

double operator * (Point a,Point b)
    {return a.x*b.y-a.y*b.x;}

inline bool operator < (Point a,Point b)
{
    double t=(a-p[1])*(b-p[1]);
    if(t==0) return dis(a,p[1])<dis(b,p[1]);
    return t<0;
}

inline void graham()
{
    int k=1;
    for(int i=2;i<=n;i++)
        if(p[k].y>p[i].y||(p[k].y==p[i].y&&p[k].x>p[i].x))
            k=i;
    swap(p[1],p[k]); sort(p+2,p+n+1);
    s[++top]=p[1];s[++top]=p[2];
    for(int i=3;i<=n;i++)
    {
        while(top>1&&(p[i]-s[top-1])*(s[top]-s[top-1])<=0)
            top--;
        s[++top]=p[i];
    }
}

inline double RC()
{
    s[top+1]=p[1]; double ans=0; int a,b;
    for(int x=1;x<=top;x++)
    {
        a=x%top+1;b=(x+2)%top+1;
        for(int y=x+2;y<=top;y++)
        {
            while(a%top+1!=y&&(s[y]-s[x])*(s[a+1]-s[x])>(s[y]-s[x])*(s[a]-s[x]))
                a=a%top+1;
            while(b%top+1!=x&&(s[b+1]-s[x])*(s[y]-s[x])>(s[b]-s[x])*(s[y]-s[x]))
                b=b%top+1;
            ans=max((s[y]-s[x])*(s[a]-s[x])+(s[b]-s[x])*(s[y]-s[x]),ans);
        }
    }
    return ans;
}

int main(int argc, char const *argv[])
{
    n=read();
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    graham();
    printf("%.3lf",RC()/2);
    return 0;
}