思路:求凸包,然后旋转卡壳对最远点对求解,注意凸包退化的情况
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
const int maxn = 1e5 + 10;
using namespace std;
struct P {
int x, y;
P() {}
P(int x, int y) : x(x), y(y) {}
P operator - (P p) {
return P(x - p.x, y - p.y);
}
int det(P a) {
return x * a.y - y * a.x;
}
int dist(P c) {
int X = c.x - x;
int Y = c.y - y;
return (X * X + Y * Y);
}
void input() {
scanf("%d %d", &x, &y);
}
};
P po[maxn], C[maxn];
int n, k;
bool cmp_x(P a, P b) {
if(a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
//凸包求解
void convex_hull() {
k = 0;
sort(po, po + n, cmp_x);
for(int i = 0; i < n; i++) {
while(k > 1 && (C[k - 1] - C[k - 2]).det(po[i] - C[k - 1]) <= 0) k--;
C[k++] = po[i];
}
for(int i = n - 2, t = k; i >= 0; i--) {
while(k > t && (C[k - 1] - C[k - 2]).det(po[i] - C[k - 1]) <= 0) k--;
C[k++] = po[i];
}
k--;
}
//旋转卡壳 -- 对踵点对求解
int solve() {
//凸包退化
if(k == 2) return C[0].dist(C[1]);
int i = 0, j = 0;
for(int kk = 0; kk < k; kk++) {
if(!cmp_x(C[i], C[kk])) i = kk;
if(cmp_x(C[j], C[kk])) j = kk;
}
int res = 0;
int si = i, sj = j;
while(si != j || sj != i) {
res = max(res, C[i].dist(C[j]));
if((C[(i + 1) % k] - C[i]).det(C[(j + 1) % k] - C[j]) < 0) i = (i + 1) % k;
else j = (j + 1) % k;
}
return res;
}
int main() {
while(scanf("%d", &n) != EOF) {
for(int i = 0; i < n; i++)
po[i].input();
convex_hull();
int res = solve();
printf("%d\n", res);
}
return 0;
}