[USACO5.1]Fencing the Cows

来源:互联网 发布:淘宝点客服没反应 编辑:程序博客网 时间:2024/05/21 17:52

裸的二维凸包

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;}
原创粉丝点击