题目描述
题解
凸包+旋转卡壳裸题
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 100005
const double eps=1e-7;
int dcmp(double x)
{
if (x<=eps&&x>=-eps) return 0;
return (x>0)?1:-1;
}
struct Vector
{
double x,y;
Vector(double X=0,double Y=0)
{
x=X,y=Y;
}
bool operator < (const Vector &a) const
{
return x<a.x||(x==a.x&&y<a.y);
}
};
typedef Vector Point;
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}
int n,top;
double ans;
Point p[N],stack[N];
double Dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b)
{
return a.x*b.y-a.y*b.x;
}
double Len(Vector a)
{
return sqrt(Dot(a,a));
}
double DisTL(Point P,Point A,Point B)
{
Vector v=B-A,w=P-A;
return fabs(Cross(v,w)/Len(v));
}
void graham()
{
sort(p+1,p+n+1);
top=0;
for (int i=1;i<=n;++i)
{
while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
--top;
stack[++top]=p[i];
}
int k=top;
for (int i=n-1;i>=1;--i)
{
while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
--top;
stack[++top]=p[i];
}
if (n>1) --top;
}
double rotating()
{
if (top==1) return 0;
if (top==2) return Len(stack[2]-stack[1]);
int now=1;
double ans=0;
for (int i=1;i<=top;++i)
{
while (dcmp(DisTL(stack[now],stack[i],stack[i%top+1])-DisTL(stack[now%top+1],stack[i],stack[i%top+1]))<=0)
now=now%top+1;
ans=max(ans,Len(stack[now]-stack[i]));
ans=max(ans,Len(stack[now]-stack[i%top+1]));
}
return ans;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%lf%lf",&p[i].x,&p[i].y);
graham();
ans=rotating();
printf("%.0lf\n",ans*ans);
}