凸包+旋转卡壳 = AC;
求凸包的时候注意去掉共线的点
刚开始做的时候纯暴力 TLE 后来想几分 剪枝优化 有无从下手最后还是老老实实的凸包+旋转卡壳了
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct Point
{
int x,y;
};
typedef struct Point point;
int cmp(point a,point b)
{
if(a.x < b.x)return 1;
else if(a.x == b.x && a.y < b.y)return 1;
return 0;
}
int Dis(point a, point b)//因为输出的时候输出的平方 所以字不开方了 后面计算也比较方便 完全不涉及浮点数 全是小数
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int Cross(point p1, point p2, point pb)
{
return (p1.x-pb.x)*(p2.y-pb.y)-(p2.x-pb.x)*(p1.y-pb.y);
}
int ConvexHull(point *p,int n,point *ch)//Andrew法寻找凸包 必须先排序
{
int m = 0;
for(int i = 0; i < n; i++)//寻找下凸包
{
while(m > 1 && Cross(ch[m-1],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],p[i],ch[m-2]) <= 0 )m--;
ch[m++] = p[i];
}
if(n > 1)m--;
return m;
}
//卡壳旋转,求出凸多边形所有对踵点
void Rotate(point *ch, int n)
{
int i, p=1;
int t1, t2, ans=0, dif;
ch[n]=ch[0];
for (i=0;i<n;i++)
{
//如果下一个点与当前边构成的三角形的面积更大,则说明此时不构成对踵点
while (fabs(Cross(ch[i],ch[i+1],ch[p+1])) > fabs(Cross(ch[i],ch[i+1],ch[p])))
p = ( p + 1 ) % n;
dif = fabs(Cross(ch[i],ch[i+1],ch[p+1])) - fabs(Cross(ch[i],ch[i+1],ch[p]));
//如果当前点和下一个点分别构成的三角形面积相等,则说明两条边即为平行线,对角线两端都可能是对踵点
if (dif == 0)
{
t1=Dis(ch[p], ch[i]);
t2=Dis(ch[p+1],ch[i+1]);
if (t1>ans)ans=t1;
if (t2>ans)ans=t2;
}
//说明p,i是对踵点
else if (dif < 0)
{
t1=Dis(ch[p], ch[i]);
if (t1>ans)ans=t1;
}
}
printf("%d\n",ans);
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int N;
scanf("%d",&N);
point f[N],t[N];
for(int i = 0; i < N; i++)
scanf("%d%d",&f[i].x,&f[i].y);
sort(f,f+N,cmp);//求凸包前先排序 以为是andrew法求凸包
int m = ConvexHull(f,N,t);
Rotate(t,m);
return 0;
}