洛谷P3829 [SHOI2012]信用卡凸包 题解

洛谷P3829 [SHOI2012]信用卡凸包 题解

题目链接:P3829 [SHOI2012]信用卡凸包

题意: 给定若干个“信用卡”,求其“凸包”周长

imgimg

这个题其实看上去很不可做,其实很简单

注意到(搬了一张图,来自link,不过这篇题解的代码好像挂了)

在这里插入图片描述

显然,由图可知,我们求的所谓凸包

就是信用卡上面那四个点组成的点集的凸包加上一个圆的面积

关于如何理解这个图形的周长恰好包括一个圆

大家就想象一只小乌龟从一点开始走,走了一大圈又回到了起点

它旋转的角度正好为 2 π 2\pi 2π

那么这道题就变成数学题了,如何处理这4个点?

再看图,可以发现这个圆对求凸包真的一点用都没有

直接把那四个点组成的新矩形当作处理对象

然后运用一点初中知识就可以求出来了

代码如下

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF ((int)0x3f3f3f3f3f3f3f3f)
#define inf ((int)0xc0c0c0c0c0c0c0c0)
#define N (int)(1e4+15)
struct vct
{
    double x,y;
}p[N<<2];
double a,b,r,ans;
int n,cnt,stk[N<<2],top,used[N<<2];
vct operator-(vct a,vct b)
{
    return (vct){a.x-b.x,a.y-b.y};
}
int cross(vct a,vct b)
{
    return a.x*b.y-a.y*b.x;
}
double dis(vct a,vct b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cout << fixed << setprecision(2);
    cin >> n >> a >> b >> r;
    a-=2*r;b-=2*r;
    double l=sqrt(a*a+b*b)/2;
    double phi=atan(a/b);
    while(n--)
    {
        double x,y,theta;
        cin >> x >> y >> theta;
        {
            double dx=l*cos(theta+phi);
            double dy=l*sin(theta+phi);
            p[++cnt]={x+dx,y+dy};
            p[++cnt]={x-dx,y-dy};
        }
        {
            double dx=l*cos(theta-phi);
            double dy=l*sin(theta-phi);
            p[++cnt]={x+dx,y+dy};
            p[++cnt]={x-dx,y-dy};
        }
    }
    sort(p+1,p+1+cnt,[](vct a,vct b)
    {
        return a.x==b.x?a.y<b.y:a.x<b.x;
    });
    stk[++top]=1;
    for(int i=2; i<=cnt; i++)
    {
        while(top>1&&cross(p[stk[top]]-p[stk[top-1]],p[i]-p[stk[top]])<=0)
            used[stk[top--]]=0;
        used[i]=1;
        stk[++top]=i;
    }
    int tmp=top;
    for(int i=cnt-1; i>=1; i--)
    {
        if(used[i])continue;
        while(top>tmp&&cross(p[stk[top]]-p[stk[top-1]],p[i]-p[stk[top]])<=0)
            used[stk[top--]]=0;
        used[i]=1;
        stk[++top]=i;
    }
    for(int i=1; i<top; i++)
        ans+=dis(p[stk[i]],p[stk[i+1]]);
    ans+=3.141592653589793*2*r;
    cout << ans << endl;
    return 0;
}

转载清说明出处

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值