旋转卡壳简介(POJ2187)(洛谷P1452)

读音

word上是这么读的:
这里写图片描述

前置技能

凸包

算法用途

旋转卡壳可以在 O(n) O ( n ) 的时间内确定一对对踵点对,它的用途包括但不限于:计算距离(凸多边形直径)、计算外接矩形(最小面积/周长)、三角剖分(洋葱三角剖分)等其他奇奇怪怪的东西。

算法实现(模板)

POJ2187洛谷P1452)为例。

这道题要我们求所有点之间的最大距离。我们先求出这些点的凸包,最大距离肯定在凸包的对踵点对上。那么我们枚举所有的对踵点对就好了,而旋转卡壳就是实现这个的。

对于每个点,我们枚举它的对踵点对。假设当前点的为 i i ,上一个点的对踵点对为x,那么当 (x+1x⃗ )×(i⃗ x⃗ )(x+1x⃗ )×(i+1x⃗ ) ( x + 1 → − x → ) × ( i → − x → ) ≥ ( x + 1 → − x → ) × ( i + 1 → − x → ) 时,说明 x x 仍不是i的对踵点对(因为 i+1 i + 1 i i 更符合)。那么把x+1并继续验证。当找到对踵点对时更新答案就好了。

代码实现(旋转卡壳为RC()):

#include<cmath>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 50005
#define F friend
#define eps 1e-7
#define sqr(x) ((x)*(x))
#define abs(x) ((x)>0?(x):-(x))
using namespace std;
struct P{
    int x,y;
    F bool operator < (P a,P b){
        return abs(a.y-b.y)<eps?a.x<b.x:a.y<b.y;
    }
    F int cs(P a,P b,P c){//叉积
        return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    }
    F int dis(P a,P b){
        return sqr(a.x-b.x)+sqr(a.y-b.y);
    }
}t[N],s[N];
int n,tp,ans;
inline char readc(){
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; return *l++;
}
inline int _read(){
    int x=0,f=1; char ch=readc();
    while (!isdigit(ch)) { if (ch=='-') f=-1; ch=readc(); }
    while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc();
    return x*f;
}
inline bool cmp(P a,P b){
    int f=cs(t[1],a,b); if (f) return f>0;
    return dis(t[1],a)-dis(t[1],b)<eps*eps;
}
inline void GR(){//凸包
    sort(t+1,t+n+1),s[++tp]=t[1],sort(t+2,t+n+1,cmp);
    for (int i=2;i<=n;s[++tp]=t[i++])
        while (tp>1&&cs(s[tp-1],s[tp],t[i])<eps) tp--;
}
inline void RC(){//旋转卡壳
    s[0]=s[tp]; int x=1;
    for (int i=0;i<tp;ans=max(ans,dis(s[i++],s[x])))
        while (cs(s[x],s[x+1],s[i])-cs(s[x],s[x+1],s[i+1])>eps)
            x=(x+1)%tp;
}
int main(){
    n=_read();
    for (int i=1;i<=n;i++)
        t[i].x=_read(),t[i].y=_read();
    return GR(),RC(),printf("%d\n",ans),0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值