洛谷3187 最小矩形覆盖(旋转卡壳)

这个精度简直石乐志

【题目分析】

这道题瞄了眼题解才知道怎么做的。。。。

关于为什么最后最优矩阵有一条边在凸包上,emmm,留坑待证。

有了上面这个结论,这道题就over了,先求凸包,再旋转卡壳找最小矩阵(左右点积,上方叉积)

精度简直被卡的想吐(-0.00000是什么?蛤?有毒吧?)。

【代码~】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int MAXN=5e5+10;
const double eps=1e-8;
using namespace std;

struct point
{
    double x,y;
    point(double a=0,double b=0){
    	x=a,y=b;
    }
    friend inline bool operator<(point a,point b){
        return abs(a.y-b.y)<eps?a.x<b.x:a.y<b.y;
    }
    friend point operator*(point a,double b){ 
		return point(a.x*b,a.y*b); 
	}
    friend point operator+(point a,point b){ 
		return point(a.x+b.x,a.y+b.y); 
	}
    friend point operator-(point a,point b){ 
		return point(a.x-b.x,a.y-b.y); 
	}
    friend double dis(point a,point b){ 
		return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 
	}
    friend double cross(point a,point b,point c){
        return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    }
    friend double dot(point a,point b,point c){
        return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
    }
}p[MAXN],q[MAXN],ans[4];

int n,last; 
double sum=1e50;

inline bool cmp(point a,point b)
{
    double ret=cross(p[1],a,b);
    if(abs(ret)>eps) 
	  return ret>0;
    return dis(a,p[1])-dis(b,p[1])<eps;
}

inline void graham()
{
    sort(p+1,p+n+1);
	q[last=1]=p[1];
	sort(p+2,p+n+1,cmp);
    for(int i=2;i<=n;q[++last]=p[i++])
    {
        while(last>1&&cross(q[last-1],q[last],p[i])<eps) 
		  last--;
	}
}

inline void rotating_celiper()
{
    int l=1,r=1,h=1; 
	q[0]=q[last];
    for(int i=0;i<last;i++)
	{
        double L,R,H,d=dis(q[i],q[i+1]);
        while(cross(q[i],q[i+1],q[h])-cross(q[i],q[i+1],q[h+1])<eps)
          h=(h+1)%last; 
		H=abs(cross(q[i],q[i+1],q[h])/d);
        while(dot(q[i],q[i+1],q[r])-dot(q[i],q[i+1],q[r+1])<eps)
            r=(r+1)%last; 
		R=dot(q[i],q[i+1],q[r])/d;
        if(!i) 
		  l=r;
        while(dot(q[i],q[i+1],q[l+1])-dot(q[i],q[i+1],q[l])<eps)
            l=(l+1)%last; 
		L=dot(q[i],q[i+1],q[l])/d;
        if((R-L)*H<sum)
		{
            sum=(R-L)*H;
            ans[0]=q[i]+(q[i+1]-q[i])*(R/d);
            ans[1]=ans[0]+(q[r]-ans[0])*(H/dis(ans[0],q[r]));
            ans[2]=ans[1]-(ans[0]-q[i])*((R-L)/dis(ans[0],q[i]));
            ans[3]=ans[2]-(ans[1]-ans[0]);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
      scanf("%lf%lf",&p[i].x,&p[i].y);
    graham();
	rotating_celiper();
	printf("%.5f\n",sum+eps);
	int dat=0;
    for(int i=1;i<=3;i++)
      if(ans[i]<ans[dat]) 
	    dat=i;
    for(int j=0;j<4;j++,dat=(dat+1)%4)
        printf("%.5f %.5f\n",ans[dat].x+eps,ans[dat].y+eps);
    return 0;
}

 

posted @ 2018-10-18 22:00  Ishtar~  阅读(186)  评论(0编辑  收藏  举报