[BZOJ1185][HNOI2007]最小矩形覆盖(计算几何-旋转卡壳)

题目:

我是超链接

题解:

这就是旋转卡壳的第二种应用了,最小矩形覆盖!
思路:矩形一定有一条边在凸包上,枚举这条边,旋转卡壳来维护其他三个点,对面的顶点用三角形面积大小判断,而两边的点则用点积来判断就好了,还有就是注意C++输出-0.00000的情况
真是不敢想象非ACM赛制的旋转卡壳怎么1A。。。这精度掉的。。。1e-9 -> 1e-8 -> 1e-7 -> 1e-10 -> AC Orz
这里写图片描述
因为up 太懒不想写题解,这里直接安利一个dalao,他的方法比我科学多了

恩科学的原因是up的代码有一种情况会被卡掉(选择的三点共线),在求交点的时候用向量真是think INF,code less,用数学老师教的联立方程的方法think less,code INF就不会被卡

代码:

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const double INF=1e18;
const double eps=1e-10;
int dcmp(double x)
{
    if (x<=eps && x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct po
{
    double x,y;
    po (double X=0,double Y=0){x=X; y=Y;}
}d[50005],sta[50005],q[5];
int n,top;double ans;
bool operator <(const po &a,const po&b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
double cj(po x,po y){return x.x*y.y-x.y*y.x;}
double dot(po x,po y){return x.x*y.x+x.y*y.y;}
po operator +(po x,po y){return po(x.x+y.x,x.y+y.y);}
po operator -(po x,po y){return po(x.x-y.x,x.y-y.y);}
po operator *(po x,double y){return po(x.x*y,x.y*y);}
double abss(double x){return (x>0)?x:-x;}
double Len(po x){return sqrt(dot(x,x));}
double disDL(po p,po a,po b){return abss(cj(p-a,b-a)/Len(b-a));}
void tb()
{
    sort(d+1,d+n+1);
    top=0;
    for (int i=1;i<=n;i++)
    {
        while (top>1 && dcmp(cj(sta[top]-sta[top-1],d[i]-sta[top-1]))<=0) top--;
        sta[++top]=d[i];     
    }
    int k=top;
    for (int i=n-1;i>=1;i--)
    {
        while (top>k && dcmp(cj(sta[top]-sta[top-1],d[i]-sta[top-1]))<=0) top--;
        sta[++top]=d[i];
    }
    if (n>1) top--;
}
void rotating()
{
    int j=2,k=2,l=2;
    sta[top+1]=sta[1];
    for (int i=1;i<=top;i++)
    {
        while (dcmp(cj(sta[i+1]-sta[i],sta[j]-sta[i]) - cj(sta[i+1]-sta[i],sta[j+1]-sta[i]))<0) j=j%top+1;
        if (k<i) k=i;
        while (dcmp(dot(sta[i+1]-sta[i],sta[k]-sta[i+1])-dot(sta[i+1]-sta[i],sta[k+1]-sta[i+1]))<0) k=k%top+1;
        if (l<j) l=j;
        while (dcmp(dot(sta[i]-sta[i+1],sta[l]-sta[i])-dot(sta[i]-sta[i+1],sta[l+1]-sta[i]))<0) l=l%top+1;
        double H=disDL(sta[j],sta[i],sta[i+1]);
        double wh=Len(sta[i+1]-sta[i]);
        double R=abss(dot(sta[i+1]-sta[i],sta[k]-sta[i+1])/wh);
        double L=abss(dot(sta[i]-sta[i+1],sta[l]-sta[i])/wh);
        double mj=H*(R+L+wh);
        if (mj<ans)
        {
            ans=mj;double sb=0;
            q[1]=sta[i]+(sta[i+1]-sta[i])*(R/wh+1);
            q[2]=sta[i+1]+(sta[i]-sta[i+1])*(L/wh+1);
            q[3]=q[2]+(sta[l]-q[2])*(H/Len(sta[l]-q[2]));
            q[4]=q[3]+(sta[i+1]-sta[i])*((R+L+wh)/wh);
        }
    }
    printf("%.5lf\n",ans+eps);
}
int main()
{
    double x,y;ans=INF;
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%lf%lf",&x,&y);
        d[i]=po(x,y);
    }
    tb();
    rotating();
    double ay=INF,ax=INF;int aw;
    for (int i=1;i<=4;i++)
      if (q[i].y<ay || (q[i].y==ay && q[i].x<ax)) ay=q[i].y,ax=q[i].x,aw=i;
    printf("%.5lf %.5lf\n",q[aw].x+eps,q[aw].y+eps);
    for (int i=3;i>=1;i--)
      printf("%.5lf %.5lf\n",q[(aw+i-1)%4+1].x+eps,q[(aw+i-1)%4+1].y+eps);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值