题意:给出n个矩形,求能覆盖所有矩形的最小的矩形的面积。
题解:对所有点求凸包,然后旋转卡壳,对没一条边求该边的最左最右和最上的三个点。
利用叉积面积求高,利用点积的性质求最左右点和长度,更新面积最小值即可。
#include#include #include #include #define MAX 50010using namespace std;struct Point{ double x,y; Point(double x=0,double y=0):x(x),y(y){}};Point P[MAX],ch[MAX];typedef Point Vector;typedef Point point;Vector operator - (Point A,Point B){ return Vector(A.x-B.x,A.y-B.y);}bool operator <(const Point &a,const Point &b){ return a.x 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 Length(Vector A){ return dot(A,A);}double rotating_calipers(Point *p,int n){ int l=1,r=1,w; double ans=1e30; p[n]=p[0]; for(int i=0;i =0) //因为边平行的时候面积相等 虽然如此但还是要继续找下一个 横着爬不动的意思 w=(w+1)%n; //找到最右的点 不可能向左的 while(dcmp(dot(p[i+1]-p[i],p[r+1]-p[i])-dot(p[i+1]-p[i],p[r]-p[i]))>0) //凸包不可能凹进去 所以不需要等号 加深对凸包求解过程的理解 r=(r+1)%n; if(i==0) l=r; while(dcmp(dot(p[i+1]-p[i],p[l+1]-p[i])-dot(p[i+1]-p[i],p[l]-p[i]))<=0) //必须加等号 否则凸包遇到直边的时候拐不过来 上不去 第二组样例可以完美说明问题 l=(l+1)%n; double d=Length(p[i+1]-p[i]); double area=fabs(Cross(p[i+1]-p[i],p[w]-p[i])) *fabs(dot(p[i+1]-p[i],p[r]-p[i])-dot(p[i+1]-p[i],p[l]-p[i]))/d; //cout< <<" "; 这里灵活运用点积求底边长度 上面那一整行化简后就是底边长度 //cout<<"rrrrr "< <<" lll "< <
分析过程: