UVA10652 Board Wrapping(凸包)

 

Sample Input
1
4
4 7.5 6 3 0
8 11.5 6 3 0
9.5 6 6 3 90
4.5 3 4.4721 2.2361 26.565
Sample Output
64.3 %

有 n 个矩形,求一个面积最小的多边形可以包含这 n 个矩形,输出这 n 个矩形占这个 n 边形的百分比 

const int N=3e3+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){}
        bool operator<(Point o)
        {
            return (o.x!=x)?x<o.x:y<o.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; }

double torad(double x)
{
    return x/180*pi;
}

Vector rotate(Vector a,double rad)
{
    return Vector(a.x*cos(rad)-a.y*sin(rad),a.y*cos(rad)+a.x*sin(rad));
}

int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n);
    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;
    rush(){
        sd(n); m=0;
        double x,y,h,w,rad,area=0;
        for(int i=0;i<n;i++){
            sff(x,y);
            sfff(w,h,rad);
            rad=-torad(rad);
            Point o(x,y);
            p[m++]=o+rotate(Vector(w/2,h/2),rad);
            p[m++]=o+rotate(Vector(w/2,-h/2),rad);
            p[m++]=o+rotate(Vector(-w/2,h/2),rad);
            p[m++]=o+rotate(Vector(-w/2,-h/2),rad);
            area+=w*h;
        }
        int vex=ConvexHull(p,m,ch);
        double ans=PolygonArea(ch,vex);
        printf("%.1lf %%\n",area*100/ans);
    }
    //PAUSE;
    return 0;
}

 

 

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