讲解见:http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html
实现:(在原有求凸包的基础上增加即可)
double cross(Pt a,Pt b,Pt c)//求叉积
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dist2(Pt a,Pt b)//求距离
{
Pt x=a-b;
return (x.norm());//因为本题只要求求距离的平方所以没有开方
}
double rotating_calipers(Poly p)//旋转卡壳
{
int j=1,n=p.pts.size();p.pts.push_back(p.pts[0]);
double ans=0;
for(int i=0;i<n;i++)
{
while(cross(p.pts[i],p.pts[i+1],p.pts[j])<cross(p.pts[i],p.pts[i+1],p.pts[j+1]))
j=(j+1)%n;
ans=max(ans,max(dist2(p.pts[i],p.pts[j]),dist2(p.pts[i+1],p.pts[j+1])));
}
return ans;
}
POJ2187:(跑得比暴力枚举O(n^2)还慢是什么鬼2333)
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>