http://poj.org/problem?id=2187
Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.
Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.
Input
* Line 1: A single integer, N
* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm
Output
* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.
Sample Input
4
0 0
0 1
1 1
1 0
Sample Output
2
Hint
Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)
题目大意:求平面最远点对的距离的平方。
思路:旋转卡壳算法求凸包直径。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const double pi=acos(-1);//弧度pi
const double eps=1e-8;//精度
inline int sgn(double x)
{
if(fabs(x)<eps)
return 0;
if(x>0)
return 1;
return -1;
}
struct point
{
double x,y;
point(double a=0,double b=0)
{
x=a,y=b;
}
friend point operator * (point a,double b)
{
return point(a.x*b,a.y*b);
}
friend point operator * (double a,point b)
{
return point(b.x*a,b.y*a);
}
point operator - (const point &b)const
{
return point(x-b.x,y-b.y);
}
point operator + (const point &b)const
{
return point(x+b.x,y+b.y);
}
point operator / (const double b)const
{
return point(x/b,y/b);
}
bool operator < (const point &b)const//按坐标排序
{
if(fabs(x-b.x)<eps)
return y<b.y-eps;
return x<b.x-eps;
}
bool operator == (const point &b)const
{
return sgn(x-b.x)==0&&sgn(y-b.y)==0;
}
void transxy(double sinb,double cosb)//逆时针旋转b弧度
{ //若顺时针 在传入的sinb前加个-即可
double tx=x,ty=y;
x=tx*cosb-ty*sinb;
y=tx*sinb+ty*cosb;
}
void transxy(double b)//逆时针旋转b弧度
{ //若顺时针传入-b即可
double tx=x,ty=y;
x=tx*cos(b)-ty*sin(b);
y=tx*sin(b)+ty*cos(b);
}
double norm()
{
return sqrt(x*x+y*y);
}
double norm2()
{
return x*x+y*y;
}
};
inline double dot(point a,point b)//点积
{
return a.x*b.x+a.y*b.y;
}
inline double cross(point a,point b)//叉积
{
return a.x*b.y-a.y*b.x;
}
inline double dist2(point a,point b)//两点间距离
{
return (a-b).norm2();
}
typedef point Vector;
vector<point> a;
struct polygon_convex//凸包
{
vector<point> p;
polygon_convex(int siz=0)
{
p.resize(siz);
}
double perimeter()//计算多边形周长
{
double sum=0;
int len=p.size();
for(int i=0;i<len-1;i++)
sum+=(p[i+1]-p[i]).norm();
sum+=(p[0]-p[len-1]).norm();
return sum;
}
double area()//计算多边形有向面积
{
double sum=0;
int len=p.size();
for(int i=0;i<len-1;i++)
sum+=cross(p[i+1],p[i]);
sum+=cross(p[0],p[len-1]);
return sum/2;
}
double final_area()//多边形面积 即一定为正数
{
return fabs(area());
}
int contain(const point b)//lgn的复杂度下判断点b是否在凸包内 true表示在凸包内(或边界上)
{
int n=p.size();
point g=(p[0]+p[n/3]+p[2*n/3])/3.0;
int l=0,r=n,mid;//二分凸包
while(l+1<r)
{
mid=(l+r)>>1;
if(sgn(cross(p[l]-g,p[mid]-g))>0)
{
if(sgn(cross(p[l]-g,b-g))>=0&&sgn(cross(p[mid]-g,b-g))<0)
r=mid;
else
l=mid;
}
else
{
if(sgn(cross(p[l]-g,b-g))<0&&sgn(cross(p[mid]-g,b-g))>=0)
l=mid;
else
r=mid;
}
}
r%=n;
int z=sgn(cross(p[r]-b,p[l]-b));
return z;//-1 内部 0 边界 1 外部
}
};
polygon_convex convex_hull()//用a中的点求解出凸包
{
polygon_convex res(2*a.size()+5);
sort(a.begin(),a.end());//按照横坐标排序
a.erase(unique(a.begin(),a.end()),a.end());//去重
int m=0;
int len=a.size();
for(int i=0;i<len;i++)//求下凸包
{
while(m>1&&sgn(cross(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0)//不包括共线点 如果包括共线点请修改此处的<=为<
--m;
res.p[m++]=a[i];
}
int k=m;
for(int i=len-2;i>=0;i--)//求上凸包
{
while(m>k&&sgn(cross(res.p[m-1]-res.p[m-2],a[i]-res.p[m-2]))<=0)//不包括共线点 如果包括共线点请修改此处的<=为<
--m;
res.p[m++]=a[i];
}
res.p.resize(m);
if(len>1)//去重
res.p.resize(m-1);
return res;
}
double convex_diameter(polygon_convex &a,int &First,int &Second)//旋转卡壳 返回最远距离 First Second 存储最远点的编号
{
vector<point> &p=a.p;
int n=p.size();
double maxd=0.0;
if(n==1)
{
First=Second=0;
return maxd;
}
p.push_back(p[0]);
for(int i=0,j=1;i<n;i++)
{
while(sgn(cross(p[i+1]-p[i],p[j]-p[i])-cross(p[i+1]-p[i],p[j+1]-p[i]))<0)
{
++j;
if(j==n)
j=0;
}
double d=dist2(p[i],p[j]);
if(d>maxd)
maxd=d,First=i,Second=j;
d=dist2(p[i+1],p[j+1]);
if(d>maxd)
maxd=d,First=i+1,Second=j+1;
}
if(First==n)
First=0;
if(Second==n)
Second=0;
p.pop_back();
return maxd;
}
int main()
{
int n;
scanf("%d",&n);
point tmp;
a.resize(n);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&tmp.x,&tmp.y);
a[i]=tmp;
}
polygon_convex pc=convex_hull();
int f,s;
printf("%lld\n",ll(convex_diameter(pc,f,s)+0.5));
return 0;
}