BZOJ 1185: [HNOI2007]最小矩形覆盖

做着做着卡住了。。。。。。

本来以为是三分的,结果发现错了。。。。。

这题不仅要枚举精度,还要枚举做法QAQ

或者说是我太弱了,根本没想到旋转卡壳。

第一次知道旋转卡壳还可以这么玩,左卡卡,右卡卡,上卡卡,哎然后一个矩形就卡好了。

听说SPJ很好玩的样子233333

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-9;
const int N=50000+5;
int dcmp(double x){
	if(fabs(x)<eps)return 0;
	return x<0?-1:1;
}
struct point{
	double x,y;
	bool operator < (const point &rhs)const{
		if(dcmp(x-rhs.x))return x<rhs.x;
		return y<rhs.y;
	}
}p[N],ch[N],rec[4];
point operator - (point a,point b){
	return (point){a.x-b.x,a.y-b.y};
}
point operator * (point a,double k){
	return (point){a.x*k,a.y*k};
}
point operator / (point a,double k){
	return (point){a.x/k,a.y/k};
}
point operator + (point a,point b){
	return (point){a.x+b.x,a.y+b.y};
}
double cross(point a,point b){
	return a.x*b.y-a.y*b.x;
}
double cross(point a,point b,point c){
	return cross(b-a,c-a);
}
double dot(point a,point b){
	return a.x*b.x+a.y*b.y;
}
double dot(point a,point b,point c){
	return dot(b-a,c-a);
}
double sqr(double x){return x*x;}
double length(point p){
	return sqrt(sqr(p.x)+sqr(p.y));
}
double length(point a,point b){
	return length(a-b);
}
double area(point a,point b,point c){
	return fabs(cross(a,b,c));
}
int n,m;
void Andrew(){
	sort(p+1,p+1+n);
	for(int i=1;i<=n;i++){
		while(m>1&&dcmp(cross(ch[m-2],ch[m-1],p[i]))<=0)m--;
		ch[m++]=p[i];
	}
	int k=m;
	for(int i=n-1;i>=1;i--){
		while(m>k&&dcmp(cross(ch[m-2],ch[m-1],p[i]))<=0)m--;
		ch[m++]=p[i];
	}
	if(n)m--;
}
double rotating(){
	double ans=1e60;
	int p=1,q=1,r=1;
	ch[m]=ch[0];
	for(int i=0;i<m;i++){
		double len=length(ch[i],ch[i+1]);
		while(dcmp(cross(ch[i],ch[i+1],ch[r])-cross(ch[i],ch[i+1],ch[r+1]))<=0)r=(r+1)%m;
		while(dcmp(dot(ch[i],ch[i+1],ch[p])-dot(ch[i],ch[i+1],ch[p+1]))<=0)p=(p+1)%m;
		if(!i)q=r;
		while(dcmp(dot(ch[i],ch[i+1],ch[q])-dot(ch[i],ch[i+1],ch[q+1]))>=0)q=(q+1)%m;
		double h=area(ch[i],ch[i+1],ch[r])/len;
		double ll=fabs(dot(ch[i],ch[i+1],ch[q])/len);
		double rl=dot(ch[i],ch[i+1],ch[p])/len;
		double w=ll+rl;
		if(h*w<ans){
			ans=h*w;
			rec[0]=ch[i]+(ch[i+1]-ch[i])*(rl/len); 
			rec[1]=rec[0]+(ch[p]-rec[0])*(h/length(rec[0],ch[p]));
			rec[2]=rec[1]+(ch[r]-rec[1])*(w/length(rec[1],ch[r]));
			rec[3]=rec[2]+(ch[q]-rec[2])*(h/length(rec[2],ch[q]));
		}
	}
	return ans;
}
int main(){
	//freopen("a.in","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	scanf("%lf%lf",&p[i].x,&p[i].y);
	Andrew();
	printf("%.5lf\n",rotating());
	int st=0;
	for(int i=1;i<4;i++){
		int d1=dcmp(rec[i].y-rec[st].y),d2=dcmp(rec[i].x-rec[st].x);
		if(d1<0)st=i;
		else if(!d1&&d2<0)st=i;
	}
	for(int i=1;i<=4;i++){
		printf("%.5lf %.5lf\n",rec[st].x,rec[st].y);
		st=(st+1)%4;
	}
	return 0;
}
			


你可能感兴趣的:(BZOJ 1185: [HNOI2007]最小矩形覆盖)