P2742 圈奶牛Fencing the Cows-凸包-模板

  • P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 10005
    struct node
    {
        double x,y;
    } s[maxn],p[maxn];
    int n,tot;
    double ans,mid;
    double cj(node a1,node a2,node b1,node b2)
    {
        return (a2.x-a1.x)*(b2.y-b1.y)-(b2.x-b1.x)*(a2.y-a1.y);
    }
    double dis(node p1,node p2)
    {
        return sqrt(((double)(p2.y-p1.y)*(p2.y-p1.y)*1.0)+((double)(p2.x-p1.x)*(p2.x-p1.x))*1.0);
    }
    bool cmp(node p1,node p2)
    {
        double temp=cj(p[1],p1,p[1],p2);
        if(temp>0)return 1;
        if(temp==0&&dis(p[0],p1)<dis(p[0],p2))return 1;
        return 0;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            if(p[i].y<p[1].y)
                swap(p[1],p[i]);
        }
        sort(p+2,p+1+n,cmp);
        s[1]=p[1];
        tot=1;
        for(int i=2; i<=n; i++)
        {
            while(tot>1&&cj(s[tot-1],s[tot],s[tot],p[i])<=0)tot--;
            s[++tot]=p[i];
        }
        ans=dis(s[1],s[tot]);
        for(int i=1; i<tot; i++)
            ans+=dis(s[i],s[i+1]);
        printf("%.2lf\n",ans);
        return 0;
    }
    
  •  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值