题目大意:平面上给定n个矩形,问用一个面积最小的凸多边形把他们包起来,计算出木板占整个包装面积的百分比;
题目解析:求一个凸包就可以了,注意角度转化;
AC代码:
#include<bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
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);}
Vector operator * (Vector A,double p) {return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A,double p) {return Vector(A.x/p,A.y/p);}
bool operator < (const Point& a,const Point& b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps=1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
bool operator == (const Point& a,const Point& b)
{
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;} //点的点积
double Length(Vector A) {return sqrt(Dot(A,A));} //向量的长度
double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));} //向量之间的角度
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;} //点的叉积
double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);} //三点构成的三角形面积的两倍
Vector Rotate(Vector A,double rad) {return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));} //向量逆时针旋转
Vector Normal(Vector A) //向量的法线
{
double L = Length(A);
return Vector(-A.y/L,A.x/L);
}
//定义直线P+tv,计算两直线的交点,前提是两直线不平行
Point GetLineIntersection(Point P,Point v,Point Q,Point w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
}
//点到直线的距离
double DistanceToLine(Point P,Point A,Point B)
{
Vector v1=B-A,v2=P-A;
return fabs(Cross(v1,v2))/Length(v1);
}
//点到线段的距离
double DistanceToSegement(Point P,Point A,Point B)
{
if(A==B) return Length(P-A);
Vector v1=B-A,v2=P-A,v3=P-B;
if(dcmp(Dot(v1,v2))<0) return Length(v2);
else if(dcmp(Dot(v1,v3))>0) return Length(v3);
else return fabs(Cross(v1,v2))/Length(v1);
}
//点在直线上的投影
Point GetLineProjection(Point P,Point A,Point B)
{
Vector v=B-A;
return A+v*(Dot(v,P-A)/Dot(v,v));
}
//判断两直线是否规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
{
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
//判断点是否在线段上并且不在线段的端点上
bool OnSegment(Point p,Point a1,Point a2)
{
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
}
//计算多边形的有向面积
double PolygonArea(Point* p,int n)
{
double area=0;
for(int i=1;i<n-1;i++)
{
area+=Cross(p[i]-p[0],p[i+1]-p[0]);
}
return area/2;
}
//凸包
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 torad(double ang)
{
return ang/180*PI;
}
const int maxn=2500;
Point p[maxn],ch[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,pc=0;
double area1=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
double x,y,w,h,j,ang;
scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&j);
Point o(x,y);
ang=-torad(j);
p[pc++]=o+Rotate(Vector(-w/2,-h/2),ang);
p[pc++]=o+Rotate(Vector(w/2,-h/2),ang);
p[pc++]=o+Rotate(Vector(-w/2,h/2),ang);
p[pc++]=o+Rotate(Vector(w/2,h/2),ang);
area1+=w*h;
}
int m=ConvexHull(p,pc,ch);
double area2=PolygonArea(ch,m);
printf("%.1lf %%\n",area1*100/area2);
}
return 0;
}