P1452 [USACO03FALL]Beauty Contest G /【模板】旋转卡壳 题解

· · 题解

前言

由于凸包求直径的时候对点执行双指针会出错,而本人不知什么原因不想写对边执行双指针的做法,于是充分发挥人类智慧,有感而发,写下这篇题解。

前置知识

二维凸包(Jarvis 算法)

思路

首先求出凸包。

如果还不会求凸包的可以先做这题。

由于要求直径,即凸包上各点的平面最远点对,所以可以想到枚举一个点,然后找到离该点最远的点,求距离。

所以问题就转化为了如何找距离一个点最远的点。

首先暴力找复杂度为 O(n^2),显然行不通(但好像同机房大佬用暴力搞过去了?)

所以考虑一些性质。

在经过不仔细地分析了几组数据之后,发现好像可以用指针按顺时针或逆时针在凸包上移动维护最远的点,于是时间复杂度降为 O(n)。

交了一发,发现被测试点 #7 hack 了。

于是继续手玩,发现了一个较小的 Hack 数据:

当 BD < AD < CD 时,在枚举到 D 点时指针会出错。

于是有一个奇怪的想法:让指针按顺时针和逆时针都枚举一遍。

但不难发现,把上面的图稍微改一改然后对称一下拼起来就是 Hack 数据。

这时候本来只要把按点指针改称按边指针就好了,但本人突然不想写边的,于是开始思考奇怪的东西。

发现一个凸包内如果要出现上面的 Hack 情况,则至少包含 2 个锐角,而多边形至多有 3 个锐角,所以上述情况出现次数极少。

于是考虑一点人类智慧:当遇到这种情况的时候类似模拟退火的做法,设置初温,有概率取更短的距离,继续指针。

不难发现这样并不会增加很多的移动次数,所以时间复杂度很优秀。

有些小细节可以看代码。

AC Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 rnd(time(NULL));

struct node{
    int x, y;
} a[100005], st[100005];

inline int read(){
    int x = 0, m = 1;
    char ch = getchar();
    while(!isdigit(ch)){
        if(ch == '-') m = -1;
        ch = getchar();
    }
    while(isdigit(ch)){
        x = x * 10 + ch - 48;
        ch = getchar();
    }
    return x * m;
}

inline void write(int x){
    if(x < 0){
        putchar('-');
        write(-x);
        return;
    }
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}

signed main() {
    auto calc = [&](node a, node b, node c) -> int {
        return (a.x - b.x) * (c.y - b.y) - (c.x - b.x) * (a.y - b.y);
    };

    auto dis = [&](node a, node b) -> int {
        return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
    };

    int n = read();

    for (int i = 1; i <= n; i++) {
        a[i].x = read(), a[i].y = read();

        if (i > 1 && (a[i].y > a[1].y || (a[i].y == a[1].y && a[i].x > a[1].x))) swap(a[i], a[1]);
    }

    sort(a + 2, a + n + 1, [&](node x, node y) {
        int z = calc(x, a[1], y);
        return (z > 0 || (z == 0 && dis(a[1], y) > dis(a[1], x)));
    });

    st[1] = a[1];
    st[2] = a[2];
    int cnt = 2;

    if (n > 2) st[++cnt] = a[3];

    for (int i = 4; i <= n; i++) {
        while (cnt > 2 && calc(st[cnt - 1], st[cnt], a[i]) >= 0) cnt--;

        st[++cnt] = a[i];
    }

    int t = 1;

    auto nxt = [&](int x) {
        return x % cnt + 1;
    };

    int ans = 0;

    for (int i = 1; i <= cnt; i++) {
        double T = 1, down = 0.4;

        ans = max(ans, dis(st[t], st[i]));

        while (dis(st[t], st[i]) < dis(st[nxt(t)], st[i]) || (dis(st[t], st[i]) >= dis(st[nxt(t)], st[i]) && exp((dis(st[nxt(t)], st[i]) - dis(st[t], st[i])) / T) < (rnd() % 100) / 100.0)) {
            t = nxt(t);

            T *= down;

            ans = max(ans, dis(st[t], st[i]));
        }
    }

    auto Nxt = [&](int x) {
        x--;

        if (!x) x = cnt;

        return x;
    };

    t = cnt;

    for (int i = cnt; i >= 1; i--) {
        double T = 1, down = 0.5;

        ans = max(ans, dis(st[t], st[i]));

        while (dis(st[t], st[i]) < dis(st[Nxt(t)], st[i]) || (dis(st[t], st[i]) >= dis(st[Nxt(t)], st[i]) && exp((dis(st[Nxt(t)], st[i]) - dis(st[t], st[i])) / T) < (rnd() % 100) / 100.0)) {
            t = Nxt(t);

            T *= down;

            ans = max(ans, dis(st[t], st[i]));
        }
    }

    write(ans);
}