POJ 3348 Cows(凸包面积)

有 n 棵树,给出他们的顶点,每头牛的活动范围为 50 平方米,以 n 棵树为顶点围成篱笆,问最多可以容纳多少头牛

求凸包面积模板题 

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

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页