Gym101606Breaking Biscuits
题意:
给你一个 N N N个点组成的多边形,你可以三维旋转它,使它能够通过一个圆柱,问圆柱底的最小直径。
思路:
这题可以转换为对于一个多边形,现在用一组平行线,使得凸多边形都在平行线内(上),求最小的平行线之间的距离。这就成了求多边形的宽。那么可以通过旋转卡壳计算,宽可以通过 max { S Δ P i P i + 1 P k } ∣ P → i P i + 1 ∣ \frac{\max \{S_{\Delta P_iP_{i+1}P_k\}}}{|\overrightarrow P_iP_{i+1}|} ∣PiPi+1∣max{SΔPiPi+1Pk}计算,与计算直径不同的部分是维护的是最小值。
代码:
#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define INF 1e18
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1);
using namespace std;
int n,top;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
struct point
{
double x,y;
point(){}
point(double xx,double yy){x=xx;y=yy;}
point operator +(point b){return point(x+b.x,y+b.y);}
point operator -(point b){return point(x-b.x,y-b.y);}
double operator ^(point b){return x*b.y-y*b.x;}//叉乘
double operator *(point b){return x*b.x+y*b.y;}//点乘
point operator *(double b){return point(x*b,y*b);}//数乘
void read(){scanf("%lf%lf",&x,&y);}
}p[N],sta[N];
int cmp1(point a,point b)
{
if(a.y==b.y)
return a.x<b.x;
else
return a.y<b.y;
}
int cmp2(point a,point b)//极角排序
{
if(atan2(a.y-sta[1].y,a.x-sta[1].x)!=atan2(b.y-sta[1].y,b.x-sta[1].x))
return (atan2(a.y-sta[1].y,a.x-sta[1].x))<(atan2(b.y-sta[1].y,b.x-sta[1].x));
return a.x<b.x;
}
double dis(point a,point b)
{
return sqrt((a-b)*(a-b));
}
void graham()
{
int i;
sort(p+1,p+n+1,cmp1);
sta[1]=p[1];
sort(p+2,p+n+1,cmp2);
sta[2]=p[2];
top=2;
for(i=3;i<=n;i++)
{
while(top>1 && ((sta[top]-sta[top-1])^(p[i]-sta[top-1]))<0)
top--;
sta[++top]=p[i];
}
return;
}
//旋转卡壳,求凸多边形宽
double rc()
{
sta[top+1]=sta[1];
int now=3,i;
double ans=INF;
for(i=1;i<=top;i++)
{
while(((sta[i+1]-sta[i])^(sta[now]-sta[i]))<((sta[i+1]-sta[i])^(sta[now+1]-sta[i])))
{
now++;
if(now==top+1)
now=1;
}
ans=min(ans,((sta[i+1]-sta[i])^(sta[now]-sta[i]))/dis(sta[i],sta[i+1]));
}
return ans;
}
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i].read();
graham();
double ans=rc();
printf("%.9lf",ans);
return 0;
}