题目链接:http://poj.org/problem?id=2187
题目大意:给出n个点,让你从中找出一对距离最大的点,输出(距离^2)。
思路:将这些点构成一个凸包,易得最大距离的点对在凸包上,因此我们对这个凸包进行旋转卡壳,然后维护最大值即可。
ACCode:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<fstream>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define Pair pair<int,int>
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// ??
//std::ios::sync_with_stdio(false);
// register
const int MAXN=5e4+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0);
const double EPS=1.0e-8;
struct Point{
double x,y,t,d;
Point(double _x=0,double _y=0,double _t=0,double _d=0){
x=_x;y=_y;t=_t;d=_d;
}
friend Point operator + (const Point &a,const Point &b){
return Point(a.x+b.x,a.y+b.y);
}
friend Point operator - (const Point &a,const Point &b){
return Point(a.x-b.x,a.y-b.y);
}
friend double operator ^ (Point a,Point b){//???????
return a.x*b.y-a.y*b.x;
}
friend int operator == (const Point &a,const Point &b){
if(fabs(a.x-b.x)<EPS&&fabs(a.y-b.y)<EPS) return 1;
return 0;
}
friend double operator * (const Point &a,const Point &b){
return a.x*b.x+a.y*b.y;
}
};
struct V{
Point start,end;double ang;
V(Point _start=Point(0,0),Point _end=Point(0,0),double _ang=0.0){
start=_start;end=_end;ang=_ang;
}
friend V operator + (const V &a,const V &b){
return V(a.start+b.start,a.end+b.end);
}
friend V operator - (const V &a,const V &b){
return V(a.start-b.start,a.end-b.end);
}
};
Point Dots[MAXN];
Point Stk[MAXN];int Top;
int n;
double Prellel(double key){
return fabs(key)<EPS?0:key;
}
double Distance(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int Cmp(Point a,Point b){
double res=Prellel(a-Dots[0]^(b-Dots[0]));
if(res>0) return 1;
if(res==0&&Distance(a,Dots[0])<Distance(b,Dots[0])) return 1;
return 0;
}
void Graham(){
int k=0;
for(int i=1;i<n;++i){
if(Dots[i].y<Dots[k].y||(Dots[i].y==Dots[k].y&&Dots[i].x<Dots[k].x)) k=i;
}swap(Dots[0],Dots[k]);sort(Dots+1,Dots+n,Cmp);
Top=1;Stk[0]=Dots[0];Stk[1]=Dots[1];
for(int i=2;i<n;++i){
while(Top>=1&&((Stk[Top]-Stk[Top-1])^(Dots[i]-Stk[Top-1]))<EPS) --Top;
Stk[++Top]=Dots[i];
}Stk[Top+1]=Stk[0];
}
double RotateCalipers(){
double ans=-INF32;int q=1;
for(int i=0;i<Top;++i){
while(((Stk[q]-Stk[i+1])^(Stk[i]-Stk[i+1]))<((Stk[q+1]-Stk[i+1])^(Stk[i]-Stk[i+1]))) q=(q+1)%Top;
ans=max(ans,max(Distance(Stk[q],Stk[i]),Distance(Stk[q+1],Stk[i+1])));
}return ans*ans;
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%lf%lf",&Dots[i].x,&Dots[i].y);
}Graham();printf("%.0lf\n",RotateCalipers());
}