【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

时间:2023-03-09 09:59:22
【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

1185: [HNOI2007]最小矩形覆盖

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

Description

【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

【旋转卡壳+凸包】BZOJ1185:[HNOI2007]最小矩形覆盖

题解

显然矩形一边一定在凸包一边上

旋转卡壳维护其他三条边经过的顶点

更新答案

这题1A欸嘿嘿

代码

//by 减维
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<bitset>
#include<set>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<ctime>
#include<algorithm>
#define LL long long
#define db double
#define inf 1<<30
#define maxn 50005
#define eps 1e-8
using namespace std; struct node{
db x,y;
}poi[maxn],sta[maxn],ans[]; int n,top;
db mn=100000000.00; bool cmp(node x,node y){if(x.x==y.x)return x.y<y.y;return x.x<y.x;}
bool cm2(node x,node y){if(x.x==y.x)return x.y>y.y;return x.x>y.x;}
node operator - (node x,node y){return (node){x.x-y.x,x.y-y.y};}
node operator + (node x,node y){return (node){x.x+y.x,x.y+y.y};}
node operator * (node x,db y){return (node){x.x*y,x.y*y};}
node operator * (db x,node y){return (node){x*y.x,x*y.y};}
db operator * (node x,node y){return x.x*y.y-x.y*y.x;}
db operator / (node x,node y){return x.x*y.x+x.y*y.y;}
bool operator == (node x,node y){return x.x==y.x&&x.y==y.y;}
bool operator > (node x,node y){if(x.y==y.y)return x.x>x.y;return x.y>y.y;}
db dis(node x,node y){return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));} void solve()
{
db ds,h,ll,rr;
int t=,l=,r=;
sta[]=sta[top];
for(int i=;i<top;++i)
{
ds=dis(sta[i],sta[i+]);
while((sta[i+]-sta[i])*(sta[t+]-sta[i])-(sta[i+]-sta[i])*(sta[t]-sta[i])>-eps)t=(t+)%top;
while((sta[i+]-sta[i])/(sta[r+]-sta[i])-(sta[i+]-sta[i])/(sta[r]-sta[i])>-eps)r=(r+)%top;
if(i==)l=r;
while((sta[i+]-sta[i])/(sta[l]-sta[i])-(sta[i+]-sta[i])/(sta[l+]-sta[i])>-eps)l=(l+)%top;
ll=(sta[i+]-sta[i])/(sta[l]-sta[i])/ds;
rr=(sta[i+]-sta[i])/(sta[r]-sta[i])/ds;
h=(sta[i+]-sta[i])*(sta[t]-sta[i])/ds;
if(mn>(rr-ll)*h){
mn=(rr-ll)*h;
ans[]=sta[i]+(sta[i+]-sta[i])*(rr/ds);
ans[]=ans[]+(sta[r]-ans[])*(h/dis(sta[r],ans[]));
ans[]=ans[]+(sta[t]-ans[])*((rr-ll)/dis(sta[t],ans[]));
ans[]=ans[]+(ans[]-ans[]);
}
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;++i)scanf("%lf%lf",&poi[i].x,&poi[i].y);
sort(poi+,poi+n+,cmp);
sta[++top]=poi[];sta[++top]=poi[];
for(int i=;i<=n;++i){
while((poi[i]-sta[top-])*(sta[top]-sta[top-])>-eps&&top>=)top--;
sta[++top]=poi[i];
}
sort(poi+,poi+n+,cm2);
int sa=top;
if(sta[top]==poi[])sta[++top]=poi[],sa--;
else sta[++top]=poi[],sta[++top]=poi[];
for(int i=;i<=n;++i){
while((poi[i]-sta[top-])*(sta[top]-sta[top-])>-eps&&top>sa)top--;
sta[++top]=poi[i];
}
top--;
solve();
printf("%.5lf\n",mn);
int fir=;
for(int i=;i<=;++i)if(ans[fir]>ans[i])fir=i;
for(int i=;i<=;++i)printf("%.5lf %.5lf\n",ans[(fir+i)%],ans[(fir+i)%]);
return ;
}