原题链接
Problem Description
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
题目大意
在平面上给出若干个顶点,要求距离最远的两个点的距离的平方。
解题思路
先求凸包,然后在凸包上枚举即可。
AC代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define PI 3.1415927
#define inf 0x3f3f3f3f
using namespace std;
struct Point
{
double x,y;
Point(double x=0, double y=0) :x(x),y(y) {}
};
typedef Point Vector;
Vector operator +(Vector A, Vector B) {return Vector(A.x+B.x,A.y+B.y);}
Vector operator -(Vector A, Vector B) {return Vector(A.x-B.x,A.y-B.y);}
Vector operator *(Vector A, double p) {return Vector(A.x*p,A.y*p);}
Vector operator /(Vector A, double p) {return Vector(A.x/p,A.y/p);}
bool operator <(const Point& a,const Point& b) {return a.x<b.x||(a.x==b.x&&a.y<b.y);}
const double eps=1e-10;
int dcmp(double x) {if(fabs(x)<eps) return 0;else return x<0?-1:1;}
bool operator ==(const Point& a,const Point& b) {return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}
double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;}
double Length(Vector A) {return sqrt(Dot(A,A));}
double Angle(Vector A,Vector B) {return acos(Dot(A,B)/Length(A)/Length(B));}
double Cross(Vector A,Vector B) {return A.x*B.y-A.y*B.x;}
double Area2(Point A,Point B,Point C) {return Cross(B-A,C-A);}
double SqrDis(Point A,Point B) {return Dot(A-B,A-B);}
Vector Rotate(Vector A,double rad)
{
return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
}
Vector Normal(Vector A)
{
double L=Length(A);
return Vector(-A.y/L,A.x/L);
}
int ConvexHull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=0;
for(int i=0;i<n;++i)
{
while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
int k=m;
for(int i=n-2;i>=0;i--)
{
while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--;
ch[m++]=p[i];
}
if(n>1) m--;
return m;
}
int main()
{
int n,m;
while(cin>>n)
{
Point ps[50005];
Point ch[50005];
rep(i,0,n) {scanf("%lf%lf",&ps[i].x,&ps[i].y);}
m=ConvexHull(ps,n,ch);
double res=-1.0;
for(int i=0;i<m;++i)
{
for(int j=0;j<i;++j)
{
res=max(res,SqrDis(ch[i],ch[j]));
}
}
printf("%.0f\n",res);
}
return 0;
}