我旋转卡壳(不然呢?,然后我 WA 了。
我枚举每一个点,记为 u,找到距离点 u 最远的点 pos,更新 res = max{ u 到 pos 的距离 }
代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
#define int long long
using namespace std;
const int N = 50010;
const int inf = 0x3f3f3f3f;
int n;
struct ppp { int x, y; } a[N];
struct stack
{
int a[N], size;
int t1() { return a[size]; }
int t2() { return a[size - 1]; }
void push(int x) { a[++size] = x; }
void pop() { size--; }
};
int c[N], cnt; /// 点的排列
int pos; /// 当前边的《对踵点》
int res;
double kkk(int i, int j) /// i 为 t2,j 为 t1
{
if (a[i].x == a[j].x) return (a[i].y > a[j].y) ? -inf : inf; /// k 最小 : k 最大
return (double)(a[i].y - a[j].y) / (double)(a[i].x - a[j].x);
}
int dis(int i, int j)
{
int xx = a[i].x - a[j].x, yy = a[i].y - a[j].y;
return xx * xx + yy * yy;
}
void solve_up()
{
stack s; s.size = 0; s.push(1);
for (int i = 2; i <= n; i++)
{
while (s.size >= 2)
{
double past = kkk(s.t2(), s.t1()), now = kkk(s.t2(), i);
if (now > past || (now == past && dis(s.t2(), i) > dis(s.t2(), s.t1()))) s.pop();
else break;
}
s.push(i);
}
for (int i = s.size - 1; i >= 2; i--) c[++cnt] = s.a[i]; /// 不算起点和终点
}
void solve_dn()
{
stack s; s.size = 0; s.push(1);
for (int i = 2; i <= n; i++)
{
while (s.size >= 2)
{
double past = kkk(s.t2(), s.t1()), now = kkk(s.t2(), i);
if (now < past || (now == past && dis(s.t2(), i) > dis(s.t2(), s.t1()))) s.pop();
else break;
}
s.push(i);
}
for (int i = 1; i <= s.size; i++) c[++cnt] = s.a[i];
}
signed main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++) scanf("%lld%lld", &a[i].x, &a[i].y);
sort(a + 1, a + n + 1, [](ppp i, ppp j) { return (i.x == j.x) ? i.y < j.y : i.x < j.x; } );
solve_dn(); solve_up();
for (int i = 2; i <= cnt; i++)
{
int _this = dis(c[i], c[1]);
if (_this > res) { res = _this; pos = i; }
}
for (int u = 2; u <= cnt; u++)
{
while (true) /// 看逆时针走会不会有更优解
{
int new_pos = pos + 1; if (new_pos > cnt) new_pos -= cnt;
int cre = dis(c[new_pos], c[u]), last = dis(c[pos], c[u]);
if (cre > last) { pos = new_pos; } else break;
}
res = max(res, dis(c[pos], c[u]));
}
printf("%lld", res);
return 0;
}