微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bzoj1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 计算几何 凸包

裸凸包,上个模板。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 10000
struct node
{
    double x,y;
}p[maxn];
int s[maxn],top,n;
double ans;
double sqr(double x)
{
    return x*x;
}
double dis(node a,node b)
{
    return sqr(a.x-b.x)+sqr(a.y-b.y);
}
inline node operator-(node a,node b)
{
    node t;t.x=a.x-b.x;t.y=a.y-b.y;return t;
}
inline double operator*(node a,node b)
{
    return a.x*b.y-a.y*b.x;
}
bool cmp(node a,node b)
{
    double t=(a-p[1])*(b-p[1]);
    if(t==0)return dis(p[1],a)<dis(p[1],b);
    return t>0;
}
void graham()
{
    int t=1;
    for(int i=2;i<=n;i++)
        if(p[i].y<p[t].y||(p[i].y==p[t].y&&p[i].x<p[t].x))t=i;
    swap(p[1],p[t]);
    sort(p+2,p+n+1,cmp);
    top=2;
    s[1]=1;s[2]=2;
    for(int i=3;i<=n;i++)
    {
        for(;(p[s[top]]-p[s[top-1]])*(p[i]-p[s[top-1]])<=0;top--);
        top++;s[top]=i;
    }
    s[top+1]=1;
    for(int i=1;i<=top;i++)
        ans+=sqrt(dis(p[s[i]],p[s[i+1]]));
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf",&p[i].x,&p[i].y);
    }
    graham();
    printf("%.2lf\n",ans);
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐