首页 > 代码库 > BZOJ1185: [HNOI2007]最小矩形覆盖

BZOJ1185: [HNOI2007]最小矩形覆盖

传送门

旋转卡壳。

首先求凸包没什么好商量的。

然后有一个结论,如果存在一个最小的矩形覆盖,那么凸包里必定存在一条边和矩形的边重合。

自己yy一下就好啦,很容易想明白。

然后枚举每条边,移动另外三条边即可。

注意点积,叉积的结合运用什么的。

//BZOJ 1185//by Cydiater//2017.1.29#include <iostream>#include <map>#include <ctime>#include <cmath>#include <cstring>#include <string>#include <cstdio>#include <cstdlib>#include <queue>#include <iomanip>#include <algorithm>#include <bitset>#include <set>#include <vector>using namespace std;#define ll long long#define up(i,j,n)	for(int i=j;i<=n;i++)#define down(i,j,n)	for(int i=j;i>=n;i--)#define cmax(a,b)	a=max(a,b)#define cmin(a,b)	a=min(a,b)#define Vector 		Point#define db		doubleconst int MAXN=1e5+5;const int oo=0x3f3f3f3f;const db eps=1e-10;const db PI=3.14159265358979323846;struct Point{	db x,y;	Point(db x=0,db y=0):x(x),y(y){}};Vector operator + (Point x,Point y){return Vector(x.x+y.x,x.y+y.y);}Vector operator - (Point x,Point y){return Vector(x.x-y.x,x.y-y.y);}Vector operator * (Vector x,db p){return Vector(x.x*p,x.y*p);}Vector operator / (Vector x,db p){return Vector(x.x/p,x.y/p);}int dcmp(db x){if(fabs(x)<eps)return 0;else return x<0?-1:1;}bool operator < (const Vector &x,const Vector &y){return dcmp(x.x-y.x)==0?x.y<y.y:x.x<y.x;}bool operator == (const Vector &x,const Vector &y){return dcmp(x.x-y.x)==0&&dcmp(x.y-y.y)==0;}int N,top;Point V[MAXN],q[MAXN],aim[10];db ans=oo;namespace solution{	void P(Point x){cout<<x.x<<‘ ‘<<x.y<<endl;}	db Cross(Vector x,Vector y){return x.x*y.y-x.y*y.x;}	db Dot(Vector x,Vector y){return x.x*y.x+x.y*y.y;}	db Len(Vector x){return sqrt(Dot(x,x));}	Point Write(){		db x,y;scanf("%lf%lf",&x,&y);		return Point(x,y);	}	void Prepare(){		scanf("%d",&N);		up(i,1,N)V[i]=Write();		sort(V+1,V+N+1);		N=unique(V+1,V+N+1)-(V+1);	}	void Andrew(){		up(i,1,N){			while(top>=2&&dcmp(Cross(q[top]-q[top-1],V[i]-q[top-1]))<=0)top--;			q[++top]=V[i];		}		int lim=top;		down(i,N-1,1){			while(top-lim>=1&&dcmp(Cross(q[top]-q[top-1],V[i]-q[top-1]))<=0)top--;			q[++top]=V[i];					}	}	Vector rotate(Vector x,db rad){return Vector(x.x*cos(rad)-x.y*sin(rad),x.x*sin(rad)+x.y*cos(rad));}	void Solve(){		Andrew();		top--;		int p2=2,p3=3,p4=4;		up(p1,1,top){			while(dcmp(Dot(q[p2+1]-q[p2],q[p1]-q[p1+1]))<0){p2%=top;p2++;}			while(dcmp(Cross(q[p3]-q[p3+1],q[p1+1]-q[p1]))>0){p3%=top;p3++;}			while(dcmp(Dot(q[p4+1]-q[p4],q[p1+1]-q[p1]))<0){p4%=top;p4++;}			db bot=fabs(Dot(q[p4]-q[p2],q[p1+1]-q[p1])/Len(q[p1+1]-q[p1]));			db hig=fabs(Cross(q[p3]-q[p1],q[p1+1]-q[p1]))/Len(q[p1+1]-q[p1]);			if(bot*hig<ans){				ans=bot*hig;				aim[1]=q[p1+1]+(q[p1+1]-q[p1])/Len(q[p1+1]-q[p1])*fabs(Dot(q[p2]-q[p1+1],q[p1+1]-q[p1])/Len(q[p1+1]-q[p1]));				aim[2]=aim[1]+rotate((q[p1+1]-q[p1])/Len(q[p1+1]-q[p1]),PI/2.0)*hig;				aim[3]=aim[2]+rotate((q[p1+1]-q[p1])/Len(q[p1+1]-q[p1]),PI)*bot;				aim[4]=aim[3]+rotate((q[p1+1]-q[p1])/Len(q[p1+1]-q[p1]),PI*1.5)*hig;			}		}		printf("%.5lf\n",ans);		int pos=0;		aim[0]=Point(oo,oo);		up(i,1,4)if(dcmp(aim[i].y-aim[pos].y)<0||(dcmp(aim[i].y-aim[pos].y)==0&&dcmp(aim[i].x-aim[pos].x)<0))pos=i;		up(i,1,4){			printf("%.5lf %.5lf\n",dcmp(aim[pos].x)==0?0:aim[pos].x,dcmp(aim[pos].y)==0?0:aim[pos].y);			pos%=4;pos++;		}	}}int main(){	//freopen("input.in","r",stdin);	using namespace solution;	Prepare();	Solve();	return 0;}

 

BZOJ1185: [HNOI2007]最小矩形覆盖