POJ 3348 Cows(凸包面积)

POJ 3348 Cows(凸包面积) 

const int N=1e4+5;

    int i,j,k;
    int n,m,t;
    struct Point
    {
        double x,y;
        Point(double x=0,double y=0):x(x),y(y){}
    }p[N],ch[N];
    typedef Point Vector;
    Vector operator+(Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
    Vector operator-(Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
    double Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; }

bool cmp(Point a,Point b)
{
    return a.x==b.x?a.y<b.y:a.x<b.x;
}

int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n,cmp);
    int m=0;
    for(int i=0;i<n;i++){
        while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0 ) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--){
        while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0 ) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}

double PolygonArea(Point *p,int n)
{
    double ans=0;
    for(int i=1;i<=n-1;i++){
        ans+=Cross(p[i]-p[0],p[i+1]-p[0]);
    }
    return ans/2;
}

int main()
{
    //IOS;
    while(~sd(n)){
        for(int i=0;i<n;i++) sff(p[i].x,p[i].y);
        int m=ConvexHull(p,n,ch);
        double ans=PolygonArea(ch,m);
        pd(int(ans/50));
    }
    //PAUSE;
    return 0;
}

 

上一篇:SQL server数据库端口访问法


下一篇:算法xio讲堂--01分数规划