bzoj1185: [HNOI2007]最小矩形覆盖 计算几何 旋转卡壳

又被卡精度。。。这概率绝了!!!

我们可以肯定凸包的一边肯定在矩形上,所以我们直接寻找上,左,右三个顶点就可以了。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define eps 1e-10
#define maxn 51000
struct node
{
    double x,y;
}p[maxn],rec[10];
double ans=1000000000000;
int n;
double sqr(double x)
{
    return x*x;
}
double dis(node a,node b)
{
    return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
node operator -(node a,node b)
{
    node t;
    t.x=a.x-b.x;t.y=a.y-b.y;
    return t;
}
node operator +(node a,node b)
{
    node t;
    t.x=a.x+b.x;t.y=a.y+b.y;
    return t;
}
double operator *(node a,node b)
{
    return a.x*b.y-a.y*b.x;
}
bool operator <(node a,node b)
{
    if(fabs(a.y-b.y)<eps) return a.x<b.x;
    return a.y<b.y;
}
bool cmp(node a,node b)
{
    double t=(a-p[1])*(b-p[1]);
    if(fabs(t)<=eps) return dis(a,p[1])<dis(b,p[1]);
    return t>0;
}
node operator*(node a,double b)
{
    node tmp;tmp.x=a.x*b;tmp.y=a.y*b;
    return tmp;
}
double operator/(node a,node b)
{
    return a.x*b.x+a.y*b.y;
}
node s[maxn];int top;
void graham()
{
    for(int i=2;i<=n;i++) if(p[i]<p[1])swap(p[i],p[1]);
    sort(p+2,p+n+1,cmp);
    s[1]=p[1];s[2]=p[2];top=2;
    for(int i=3;i<=n;i++)
    {
        for(;top>1 && (s[top]-s[top-1])*(p[i]-s[top-1])<=0;top--);
        top++;s[top]=p[i];
    }
    s[top+1]=p[1];
    //printf("%d\n",top);
}
double get(int a,int b,int c)
{
    double tmp=(s[a]-s[b])*(s[a]-s[c]);
    return fabs(tmp);
}
double check(int a,int b,int c)
{
    return (s[a]-s[b])/(s[c]-s[b]);
}
void rc()
{
    int l=1,r=1,m=1;
    double L,R,D,H;
    for(int i=1;i<=top;i++)
    {
        D=dis(s[i],s[i+1]);

        while(get(m+1,i,i+1)-get(m,i,i+1)>-eps ) m=m%top+1;
        while(check(r+1,i,i+1)-check(r,i,i+1)>-eps) r=r%top+1;
        if(i==1) l=r;
        while(check(l+1,i+1,i)-check(l,i+1,i)>-eps) l=l%top+1;
        L=(s[i+1]-s[i])/(s[l]-s[i])/D;
        R=(s[i+1]-s[i])/(s[r]-s[i])/D;
		H=fabs( (s[i+1]-s[i])*(s[m]-s[i])/D );
		double tmp=(R-L)*H;
		if(tmp<ans)
		{
		    ans=tmp;
			rec[0]=s[i]+(s[i+1]-s[i])*(R/D);
			rec[1]=rec[0]+(s[r]-rec[0])*(H/dis(rec[0],s[r]));
			rec[2]=rec[1]-(rec[0]-s[i])*((R-L)/dis(s[i],rec[0]));
			rec[3]=rec[2]-(rec[1]-rec[0]);
		}
    }
}
int main()
{
    scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
	graham();

	rc();
	printf("%.5lf\n",ans);
	int tmp=0;
	for(int i=1;i<=3;i++)
		if(rec[i]<rec[tmp])tmp=i;
	for(int i=0;i<=3;i++)
		printf("%.5lf %.5lf\n",rec[(i+tmp)%4].x,rec[(i+tmp)%4].y);
    return 0;
}
/*
6
1 3
1 4
2 1
3 0
3 6
6 3
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值