[USACO5.1]Fencing the Cows

裸的二維凸包

namespace point
{
    struct point
    {
        double x,y;
        point()
        {
            x=y=0;
        }
        point makepoint(double x,double y)
        {
            point k;
            k.x=x;
            k.y=y;
            return k;
        }
        point operator - (point a)
        {
            return makepoint(x-a.x,y-a.y);
        }
    }g[10010];
    int len,s[10010];
    bool cmp1(point a,point b)
    {
        return a.y<b.y;
    }
    bool cmp2(point a,point b)
    {
        return a.x*b.y<a.y*b.x;
    }
    bool cmp3(point c,point b,point a)
    {
        return !cmp2(a-c,b-c);
    }
    void insert(int x)
    {
        s[++len]=x;
    }
    void popfrom(int x)
    {
        while(len>2&&cmp3(g[x],g[s[len]],g[s[len-1]]))
            len--;
    }
    double sqr(double x)
    {
        return x*x;
    }
    double dis(point a,point b)
    {
        return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
    }
    double calc()
    {
        double ans=dis(g[s[1]],g[s[len]]);
        fr(i,2,len)
            ans+=dis(g[s[i]],g[s[i-1]]);
        return ans;
    }
}
int n;
int main()
{
    n=read();
    point::point *a=point::g;
    fr(i,1,n)
        scanf("%lf%lf",&(a+i)->x,&(a+i)->y);
    sort(a+1,a+n+1,point::cmp1);
    fd(i,n,1)
        a[i]=a[i]-a[1];
    sort(a+2,a+n+1,point::cmp2);
    fr(i,1,2)
        point::insert(i);
    fr(i,3,n)
    {
        point::popfrom(i);
        point::insert(i);
    }
    printf("%.2lf\n",point::calc());
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章