旋转卡壳求宽度(求凸包的宽度)

思路链接:

https://blog.csdn.net/ACMaker/article/details/3177292

题目链接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=6177

This is the codes:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
const int maxn = 2e5+100;
struct Point
{
    LL x,y;
    Point() {}
    Point(LL _x,LL _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(Point b) const
    {
        return Point(x - b.x, y - b.y);
    }
    LL operator ^ (Point b) const   //计算叉积
    {
        return x*b.y - y*b.x;
    }
} s[maxn],ch[maxn];
int top,n,m;
LL cs(Point p0,Point p1,Point p2)   //向量p0->p1 叉乘  向量p0->p2
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double pow_2(double x)//平方
{
    return x*x;
}
double dis(Point p1,Point p2)//两点距离
{
    return sqrt(pow_2(p1.x-p2.x)+pow_2(p1.y-p2.y));
}
bool cmp(Point p1,Point p2)
{
    LL tmp = cs(s[0],p1,p2);
    if(tmp>0)
        return 1;
    else if(tmp<0)
        return 0;
    else
        return dis(s[0],p1)<dis(s[0],p2);//叉积为零,表示垂直
}
void graham()    //生成凸包,ch从0开始存放
{
    if(n==1)
        ch[top++] = s[0];
    else
    {
        for(int i = 0; i < n; i ++)
        {
            while(top>1&&cs(ch[top-2],ch[top-1],s[i])<=0)
                top --;
            ch[top++] = s[i];
        }
    }
}
double len(Point a,Point b,Point c)    //两点式情况下点到直线的距离
{
    double s = abs(cs(a,b,c));
    return s/dis(a,b);
}
double cps()      //旋转卡壳
{
    if(top<=2)
        return 0;   //判断凸包退化成线段的情况
    double ans = LL_INF;
    int cur = 1;
    for(int i = 1; i <= top; i ++)
    {
        Point v = ch[i-1] - ch[i%top];
        while((v^(ch[(cur+1)%top]-ch[cur]))<0)
        {
            cur = (cur+1)%top;
        }
        ans = min(ans,len(ch[i-1],ch[i%top],ch[cur]));
        //使用min求的是旋转卡壳的宽度,
    }
    return ans;
}
int main()
{
    //freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
    //freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
    while(~scanf("%d%d",&n,&m))
    {
        top=0;
        Point p0(0,1e9);
        int k;
        for(int i = 0; i < n; i ++)
        {
            scanf("%lld%lld",&s[i].x,&s[i].y);
            if(p0.y>s[i].y||(p0.y==s[i].y&&p0.x>s[i].x))
            {
                p0 = s[i];
                k = i;
            }
        }
        swap(s[0],s[k]);
        sort(s+1,s+n,cmp);  //极角排序,跳过第0个
        graham();
        printf("%.15lf\n",cps());
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值