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;
}

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...