题解
比较显然的一道几何题,思维过程也比较简单,关键点在于想清楚最小矩形的一条边一定在凸包上(卡得最紧)。建好凸包后,在其上旋转卡壳,维护矩形另外三条边需经过的三个点。恶心的地方在于卡精度(在1*1的范围内竟然有万多个点),不知为何,在同样的计算方法下,我把凸包最后一个点提到最前讨论就过了……
代码
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
typedef double db;
const int maxn=5e4+5;
const db eps=1e-8;
int n,top,st=1;
db ans=1e60;
struct Point
{
db x,y;
Point(){}
Point(db x,db y):x(x),y(y){}
}p[maxn],s[maxn],ver[5];
typedef Point Vector;
int dcmp(db a)
{
if(fabs(a)<eps) return 0;
return a<0?-1:1;
}
bool operator < (const Point& a,const Point& b)
{
return dcmp(a.x-b.x)<0||(dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)<0);
}
Point operator + (const Point& a,const Vector& b)
{
return Point(a.x+b.x,a.y+b.y);
}
Vector operator - (const Point& a,const Point& b)
{
return Vector(a.x-b.x,a.y-b.y);
}
Vector operator * (const Vector& a,const db& b)
{
return Vector(a.x*b,a.y*b);
}
Vector operator / (const Vector& a,const db& b)
{
return Vector(a.x/b,a.y/b);
}
db Cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
db Dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
db Length(Vector a)
{
return sqrt(Dot(a,a));
}
void Andrew()
{
sort(p+1,p+1+n),s[++top]=p[1];
for(int i=2;i<=n;i++)
{
while(top>1&&dcmp(Cross(s[top]-s[top-1],p[i]-s[top-1]))<=0) top--;
s[++top]=p[i];
}
int temp=top;
for(int i=n-1;i;i--)
{
while(top>temp&&dcmp(Cross(s[top]-s[top-1],p[i]-s[top-1]))<=0) top--;
s[++top]=p[i];
}
top--,s[0]=s[top];
}
void RC()
{
for(int i=0,j=1,l=1,r=1;i<top;i++)
{
Vector v=s[i+1]-s[i],w;
db dis=Length(v);
while(dcmp(Cross(v,s[j+1]-s[i])-Cross(v,s[j]-s[i]))>=0) j=j%top+1;
while(dcmp(Dot(v,s[r+1]-s[i])-Dot(v,s[r]-s[i]))>=0) r=r%top+1;
l=j;
while(dcmp(Dot(v,s[l+1]-s[i])-Dot(v,s[l]-s[i]))<=0) l=l%top+1;
db h=fabs(Cross(v,s[j]-s[i]))/dis;
db wl=Dot(v,s[l]-s[i])/dis,wr=Dot(v,s[r]-s[i])/dis;
db ww=wr-wl,area=ww*h;
if(area<ans)
{
ans=area;
ver[1]=s[i]+v*(wr/dis);
w=s[r]-ver[1],ver[2]=ver[1]+w*(h/Length(w));
ver[3]=ver[2]-v*(ww/dis);
ver[4]=ver[1]+(ver[3]-ver[2]);
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
Andrew(),RC();
printf("%.5lf\n",ans);
for(int i=1;i<=4;i++)
{
if(dcmp(ver[i].y-ver[st].y)<0||(dcmp(ver[i].y-ver[st].y)==0&&dcmp(ver[i].x-ver[st].x)<0)) st=i;
ver[i+4]=ver[i];
}
for(int i=1;i<=4;i++) printf("%.5lf %.5lf\n",ver[i+st-1].x,ver[i+st-1].y);
return 0;
}