poj2187 旋转卡(qia)壳(ke)

时间:2022-05-02 18:32:56

题意:求凸包的直径

关于对踵点对、旋转卡壳算法的介绍可以参考这里:

http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html

http://www.cppblog.com/staryjy/archive/2009/11/19/101412.html

http://blog.csdn.net/ACMaker

这里使用了lrj的complex<double>大法来表示复数。

注意别忘了复数乘法的定义:(a+bi)*(c+di)=(ac-bd)+(bc+ad)i

 #include <iostream>
#include <complex>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef complex<double> Point; //Point A:complex x+yi
typedef Point Vector;
const double eps=1e-; int dcmp(double x) //return 0:x==0 -1:x<0 1:x>0
{
if (fabs(x)<eps) return ;
else return x<?-:;
} bool operator == (const Point &A,const Point &B)
{
double ax=real(A),ay=imag(A),bx=real(B),by=imag(B);
return dcmp(ax-bx)== && dcmp(ay-by)==;
}
/*
bool operator < (const Point &A,const Point &B)
{
double ax=real(A),ay=imag(A),bx=real(B),by=imag(B);
return ((ax<bx)||(ax==bx && ay<by));
}
*/
bool cmp(const Point &A,const Point &B)
{
double ax=real(A),ay=imag(A),bx=real(B),by=imag(B);
//return ((ax<bx)||(ax==bx && ay<by));
int dx=dcmp(ax-bx),dy=dcmp(ay-by); //return 0:ax==bx -1:ax<bx 1:ax>bx
return ((dx==-)||((dx==)&&(dy==-)));
} double Dot(Vector A,Vector B) //Ax*Bx+Ay*By
{
return real(conj(A)*B);
} double Cross(Vector A,Vector B) //Ax*By-Ay*Bx
{
return imag(conj(A)*B);
} Vector Rotate(Vector A,double rad)
{
return A*exp(Point(,rad));
} double Dist(Point A,Point B) //distance^2
{
double ax=real(A),ay=imag(A),bx=real(B),by=imag(B);
//cout<<ax<<" "<<ay<<" "<<bx<<" "<<by<<" "<<(ax-bx)*(ax-bx)+(ay-by)*(ay-by)<<endl;
return ((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
} double PolygonArea(Point *p,int n)
{
double area=;
for (int i=;i<n-;i++)
area+=Cross(p[i]-p[],p[i+]-p[]);
return area/;
} int convexhull(Point *p,int n,Point *ch)
{
sort(p,p+n,cmp);
int m=;
for (int i=;i<n;i++)
{
while (m> && Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)
m--;
ch[m++]=p[i];
}
int k=m;
for (int i=n-;i>=;i--)
{
while (m>k && Cross(ch[m-]-ch[m-],p[i]-ch[m-])<=)
m--;
ch[m++]=p[i];
}
if (n>) m--;
return m;
} double rotating_calipers(Point *ch,int num)
{
int q=;
double ans=;
ch[num]=ch[];
for(int p=;p<num;p++)
{
while(Cross(ch[p+]-ch[p],ch[q+]-ch[p])>Cross(ch[p+]-ch[p],ch[q]-ch[p]))
q=(q+)%num;
ans=max(ans,max(Dist(ch[p],ch[q]),Dist(ch[p+],ch[q+])));
}
return ans;
} Point p[],ch[];
int n,x,y; int main()
{
//freopen("in.txt","r",stdin);
//freopen("ou.txt","w",stdout); while (cin>>n)
{
for (int i=;i<n;i++)
{
cin>>x>>y;
p[i]=Point(x,y);
} int num=convexhull(p,n,ch);
int ans=rotating_calipers(ch,num); //cout<<num<<" "<<ans<<endl; cout<<ans<<endl;
}
return ;
}

发现一个下载USACO Contest数据的好地方:http://iskren.info/tasks/USACO/