bzoj 1185: [HNOI2007]最小矩形覆盖 (旋转卡壳)

1185: [HNOI2007]最小矩形覆盖

Time Limit: 10 Sec   Memory Limit: 162 MBSec   Special Judge
Submit: 1406   Solved: 633
[ Submit][ Status][ Discuss]

Description

 

Input

Output

Sample Input

Sample Output

HINT

Source

[ Submit][ Status][ Discuss]

题解:与hdu 5251 类似,不过要求正方形的顶点。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-8
#define N 100003
using namespace std;
int n,m;
struct vector {
	double x,y;
	vector (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],ch[N],line[5];
double pi=acos(-1.0),ans;
typedef vector point;
struct data{
	double ans;
	point a,b,c,d;
}tmp;
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 t){
	return vector (a.x*t,a.y*t);
}
vector operator /(vector a,double t){
	return vector (a.x/t,a.y/t);
}
bool operator <(vector a,vector b){
	return a.x<b.x||a.x==b.x&&a.y<b.y;
}
int dcmp(double x){
	if (fabs(x)<eps) return 0;
	return x<0?-1:1;
}
double cross(vector a,vector b){
	return a.x*b.y-a.y*b.x;
}
double dot(vector a,vector b){
	return a.x*b.x+a.y*b.y;
}
void convexhull()
{
	m=0;
	sort(a+1,a+n+1);
	if (n==1) {
		ch[m++]=a[1];
		return;
	}
	for (int i=1;i<=n;i++){
		while (m>1&&cross(ch[m-1]-ch[m-2],a[i]-ch[m-2])<=0) m--;
		ch[m++]=a[i];
	}
	int k=m;
	for (int i=n-1;i>=1;i--){
		while (m>k&&cross(ch[m-1]-ch[m-2],a[i]-ch[m-2])<=0) m--;
		ch[m++]=a[i];
	}
	m--;
}
double len(vector a){
	return sqrt(a.x*a.x+a.y*a.y);
}
double distl(point p,point a,point b){
	vector v=b-a; vector u=p-a;
	return fabs(cross(v,u))/len(v);
}
vector rotate(vector a,double rad)
{
	return vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
point glt(point a,point b,point c,point d)
{
	//cout<<a.x<<" "<<a.y<<endl;
//	cout<<b.x<<" "<<b.y<<endl;
	//cout<<c.x<<" "<<c.y<<endl;
	//cout<<d.x<<" "<<d.y<<endl;
	vector v=b-a; vector w=d-c;
	vector u=a-c;
	double t=cross(w,u)/cross(v,w);
	return a+v*t;
}
void update(point a,point b,point k,point c,point d)
{
	vector v=b-a;
	double t=dot(b-a,c-b)/len(b-a);
	vector l=v/len(b-a)*t;
	tmp.b=b+l; v=a-b;
	t=dot(a-b,d-a)/len(b-a);
	l=v/len(b-a)*t;
	tmp.a=a+l; v=b-a;
	vector u=rotate(v,pi/2)/len(b-a)*distl(k,a,b);
	point a1=a+u,b1=b+u;
	tmp.c=glt(a1,b1,c,tmp.b);
	tmp.d=glt(a1,b1,d,tmp.a);
}
void rotating()
{
	int i,j,k;
	for (int t=2;t<m;t++)
	 if (t==2||distl(ch[t],ch[0],ch[1])>distl(ch[k],ch[0],ch[1])) k=t;
	for (int t=2;t<m;t++)
	 if (t==2||dot(ch[1]-ch[0],ch[t]-ch[1])>=dot(ch[1]-ch[0],ch[i]-ch[1])) i=t;
	for (int t=2;t<m;t++)
	 if (t==2||dot(ch[0]-ch[1],ch[t]-ch[0])>=dot(ch[0]-ch[1],ch[j]-ch[0])) j=t;
	double h=distl(ch[k],ch[0],ch[1]);
	double l=len(ch[0]-ch[1])+fabs(dot(ch[1]-ch[0],ch[i]-ch[1]))/len(ch[0]-ch[1])+fabs(dot(ch[0]-ch[1],ch[j]-ch[0]))/len(ch[0]-ch[1]);
	ans=h*l; tmp.ans=ans;
	update(ch[0],ch[1],ch[k],ch[i],ch[j]);
	ch[m]=ch[0];
    for (int t=1;t<m;t++){
    	while (distl(ch[k],ch[t],ch[t+1])<=distl(ch[(k+1)%m],ch[t],ch[t+1])) k=(k+1)%m;
    	while (dot(ch[t+1]-ch[t],ch[i]-ch[t+1])<=dot(ch[t+1]-ch[t],ch[(i+1)%m]-ch[t+1])&&(i+1)%m!=t+1) i=(i+1)%m;
    	while (dot(ch[t]-ch[t+1],ch[j]-ch[t])<=dot(ch[t]-ch[t+1],ch[(j+1)%m]-ch[t])&&(j+1)%m!=t) j=(j+1)%m;
    	h=distl(ch[k],ch[t],ch[t+1]);
    	l=len(ch[t]-ch[t+1])+fabs(dot(ch[t+1]-ch[t],ch[i]-ch[t+1]))/len(ch[t]-ch[t+1])+fabs(dot(ch[t]-ch[t+1],ch[j]-ch[t]))/len(ch[t]-ch[t+1]);
    	if (h*l<ans) 
		 update(ch[t],ch[t+1],ch[k],ch[i],ch[j]),tmp.ans=h*l,ans=h*l;
	}
}
int cmp(int x,int y)
{
	return line[x].y<line[y].y||line[x].y==line[y].y&&line[x].x<line[y].x;
}
int main()
{
	freopen("a.in","r",stdin);
	//freopen("my.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lf%lf\n",&a[i].x,&a[i].y);
	convexhull();
	//for (int i=0;i<m;i++) cout<<ch[i].x<<" "<<ch[i].y<<endl;
	ans=1e60;
	rotating();
	printf("%.5lf\n",tmp.ans);
	int rank[5];
	for (int i=1;i<=4;i++) rank[i]=i;
	line[1]=tmp.a; line[2]=tmp.b; line[3]=tmp.c; line[4]=tmp.d;
	sort(rank+1,rank+4+1,cmp);
	for (int i=rank[1];i<=4;i++) printf("%.5lf %.5lf\n",line[i].x,line[i].y);
	for (int i=1;i<rank[1];i++) printf("%.5lf %.5lf\n",line[i].x,line[i].y);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值