题解:P1452 [USACO03FALL] Beauty Contest G

· · 题解

前言

怎么没有模拟退火的题解?我来一篇。

欸?好像可以直接人类智慧。那这道题就当成随机化的练手题吧。

题意

给定平面上 n 个点,求最远的两点的距离。

思路

看到 |x|,|y|\le 10^4 这个条件,马上就能想到模拟退火。我们考虑随机一个点 (x, y),然后对于这个点,我们找出离它最远的两点 (i, j),假设这两个点到 (x, y) 的距离分别为 Max1, Max2,则我们认为 i, j 的距离为 Max1 + Max2。显然,这是错的,因此我们要求出所有的 Max1 + Max2 的最小值。具体代码见下:

:::info[57 pts code]

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using dl = long double;

const int N = 2e5 + 5;
const dl eps = 1e-10, D = 0.9991;
const ll MAX_ = 2147483648; // 因为 mt19937 的范围是 0 ~ 2147483648 * 2

ll n;
dl ansx, ansy, x, y, ans = 4e9, now = 4e9, Maxx, Maxy, Minx = 1e9, Miny = 1e9; // ans 为最优答案,now 为概率接受的答案。ansx, ansy 为 ans 对应的坐标,x, y 为 now 对应的坐标。
mt19937 gen(time(0));

struct tp{
    dl x, y;
} a[N];

inline dl rng(dl C){ // 随机一个 ((-1 ~ 1) * C) 的数
    ll x = gen();
    x -= MAX_;
    dl y = x;
    y /= (1.0 * MAX_);
    y *= C;
    return y;
}

dl calc(dl x, dl y){
    dl Max1 = -2e9, Max2 = -2e9; // 其实就是最大和次大
    for(int i = 1; i <= n; i++){
        dl Dis = sqrt((a[i].x - x) * (a[i].x - x) + (a[i].y - y) * (a[i].y - y)); // (x, y) 到 (a[i].x, a[i].y) 的距离
        if(Dis >= Max1){
            Max2 = Max1, Max1 = Dis;
        }else{
            Max2 = max(Max2, Dis);
        }
    }
    return Max1 + Max2; // 认为距离为 Max1 + Max2
}

inline void SA(){
    dl C = 2e8;
    x = ansx, y = ansy; // 初始化 x, y
    while(((double)clock() / CLOCKS_PER_SEC) < 0.99 && C > eps){
        dl x_ = x + rng(C), y_ = y + rng(C);
        dl ans_ = calc(x_, y_);
        if(x_ < Minx || y_ < Miny || x_ > Maxx || y_ > Maxy){ // 超过边界了,肯定不可能是答案
            C *= D;
            continue;
        }
        if(ans_ < ans){ // 更优就接受
            ans = ans_;
            ansx = x_, ansy = y_;
        }
        if(ans_ < now){
            x = x_, y = y_;
            now = ans_;
        }
        else if(gen() <= exp(1.0 * (now - ans_) / C) * gen.max()){ // 概率接受
            x = x_, y = y_;
            now = ans_;
        }
        C *= D;
    }
}

int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1, x_, y_; i <= n; i++){
        cin >> x_ >> y_;
        a[i] = {x_, y_};
        ansx += x_, ansy += y_;
        Minx = min(Minx, a[i].x); // 计算给定的点的范围
        Maxx = max(Maxx, a[i].x);
        Miny = min(Miny, a[i].y);
        Maxy = max(Maxy, a[i].y);
    }
    ansx /= (1.0 * n);
    ansy /= (1.0 * n);
    ans = now = calc(ansx, ansy); // 先对所有点的平均点算一遍答案,增加正确性,以及初始化 ansx, ansy, x, y。
    while(((double)clock() / CLOCKS_PER_SEC) < 0.99){
        SA();
    }
    cout << (ll)(ans * ans + 0.5); // + 0.5 是因为强转 long long 会向下取整
    return 0;
}

:::

交上去,果然,只有 57 分。很显然,是我们正确性太低,这种做法对我们只有一点启发性。

因为刚做了面试的考验,它的人类智慧做法启发了我。我们考虑换一种方式计算:对于随机出的坐标 (x, y),我们记录每个点 i 到它的距离 dis_i,然后降序排序,再枚举每个点,取刚刚我们统计的 dis 的前 10 大的距离对应的点,和当前点算一下距离,最后算出的最大值就作为答案。为了保证正确性,我们还可以升序排序再做一遍,每次取 dis 前 10 小的点来算。

代码里有注释。

:::info[100 pts code]

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using dl = long double;

const int N = 2e5 + 5;
const dl eps = 1e-15, D = 0.9991, I = 1.0;
const ll MAX_ = 2147483648; // 因为 mt19937 的范围是 0 ~ 2147483648 * 2

ll n;
int tot;
dl ansx, ansy, x, y, ans = -4e9, now = -4e9, Maxx, Maxy, Minx = 1e9, Miny = 1e9;// ans 为最优答案,now 为概率接受的答案。ansx, ansy 为 ans 对应的坐标,x, y 为 now 对应的坐标。
mt19937 gen(time(0));

struct Tp{
    dl x, y, dis;
} dis[N];

struct tp{
    dl x, y;
} a[N];

bool cmp(const Tp &i, const Tp &j){
    return i.dis > j.dis;
}

bool cmp2(const Tp &i, const Tp &j){
    return i.dis < j.dis;
}

inline dl rng(dl C){ // 随机一个 ((-1 ~ 1) * C) 的数
    ll x = gen();
    x -= MAX_;
    dl y = x;
    y /= (1.0 * MAX_);
    y *= C;
    return y;
}

dl calc(dl x, dl y){
    for(int i = 1; i <= n; i++){
        dis[i] = {a[i].x, a[i].y, sqrt((a[i].x - x) * (a[i].x - x) + (a[i].y - y) * (a[i].y - y))}; // 计算每个点到 (x, y) 的距离
    }
    sort(dis + 1, dis + n + 1, cmp); // 降序排序
    dl ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= min(n, 10ll); j++){
            ans = max(ans, sqrt((dis[j].x - a[i].x) * (dis[j].x - a[i].x) + (dis[j].y - a[i].y) * (dis[j].y - a[i].y))); // 算最大距离
        }
    }
    sort(dis + 1, dis + n + 1, cmp2); // 升序排序
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= min(n, 10ll); j++){
            ans = max(ans, sqrt((dis[j].x - a[i].x) * (dis[j].x - a[i].x) + (dis[j].y - a[i].y) * (dis[j].y - a[i].y)));
        }
    }
    return ans;
}

inline void SA(){
    dl C = max(I, Maxy - Miny) * max(I, Maxx - Minx);
    x = ansx, y = ansy; // 初始化 x, y
    while(((double)clock() / CLOCKS_PER_SEC) < 0.98 && C > eps){
        dl x_ = x + rng(C), y_ = y + rng(C);
        if(x_ < Minx || y_ < Miny || x_ > Maxx || y_ > Maxy){ // 随机出的点超出范围了,没必要算
            C *= D;
            continue;
        }
        dl ans_ = calc(x_, y_);
        if(ans_ > ans){ // 更优就接受
            ans = ans_;
            ansx = x_, ansy = y_;
        }
        if(ans_ > now){ // 更优就接受
            x = x_, y = y_;
            now = ans_;
        }
        else if(gen() <= exp(1.0 * (now - ans_) / C) * gen.max()){ // 概率接受
            x = x_, y = y_;
            now = ans_;
        }
        C *= D;
    }
}

int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1, x_, y_; i <= n; i++){
        cin >> x_ >> y_;
        a[i] = {x_, y_};
        ansx += x_, ansy += y_;
        Minx = min(Minx, a[i].x); // 计算点的范围
        Maxx = max(Maxx, a[i].x);
        Miny = min(Miny, a[i].y);
        Maxy = max(Maxy, a[i].y);
    }
    ansx /= (1.0 * n);
    ansy /= (1.0 * n);
    ans = now = calc(ansx, ansy); // 先对所有点的平均点算一遍答案,增加正确性,以及初始化 ansx, ansy, x, y。
    while(((double)clock() / CLOCKS_PER_SEC) < 0.98){
        SA();
    }
    cout << (ll)(ans * ans + 0.5); // + 0.5 是因为强转 long long 会向下取整
    return 0;
}

:::

对了,这里再简单介绍一个函数。

shuffle 函数

shuffle 函数于 C++11 引入,效用同 random_shuffle,即随机打乱序列。时间复杂度为 O(n)。

当我们想要随机打乱一个序列,只需要使用以下代码:

mt19937 gen(time(0));

...

shuffle(a + 1, a + n + 1, gen);

因此,shuffle 函数需要与 mt19937 一起使用。对比 random_shuffle,它的优点在于,shuffle 的随机性更好。

回到本题,我们将上面的排序换成 shuffle,同样可以通过。当然,两种方法合起来一起用自然正确性更高。

最终代码:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using dl = long double;

const int N = 2e5 + 5;
const dl eps = 1e-15, D = 0.9991, I = 1.0;
const ll MAX_ = 2147483648; // 因为 mt19937 的范围是 0 ~ 2147483648 * 2

ll n;
int tot;
dl ansx, ansy, x, y, ans = -4e9, now = -4e9, Maxx, Maxy, Minx = 1e9, Miny = 1e9;// ans 为最优答案,now 为概率接受的答案。ansx, ansy 为 ans 对应的坐标,x, y 为 now 对应的坐标。
mt19937 gen(time(0));

struct Tp{
    dl x, y, dis;
} dis[N];

struct tp{
    dl x, y;
} a[N];

bool cmp(const Tp &i, const Tp &j){
    return i.dis > j.dis;
}

bool cmp2(const Tp &i, const Tp &j){
    return i.dis < j.dis;
}

inline dl rng(dl C){ // 随机一个 ((-1 ~ 1) * C) 的数
    ll x = gen();
    x -= MAX_;
    dl y = x;
    y /= (1.0 * MAX_);
    y *= C;
    return y;
}

dl calc(dl x, dl y){
    for(int i = 1; i <= n; i++){
        dis[i] = {a[i].x, a[i].y, sqrt((a[i].x - x) * (a[i].x - x) + (a[i].y - y) * (a[i].y - y))}; // 计算每个点到 (x, y) 的距离
    }
    sort(dis + 1, dis + n + 1, cmp); // 降序排序
    dl ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= min(n, 10ll); j++){
            ans = max(ans, sqrt((dis[j].x - a[i].x) * (dis[j].x - a[i].x) + (dis[j].y - a[i].y) * (dis[j].y - a[i].y))); // 算最大距离
        }
    }
    sort(dis + 1, dis + n + 1, cmp2); // 升序排序
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= min(n, 10ll); j++){
            ans = max(ans, sqrt((dis[j].x - a[i].x) * (dis[j].x - a[i].x) + (dis[j].y - a[i].y) * (dis[j].y - a[i].y)));
        }
    }
    shuffle(dis + 1, dis + n + 1, gen); // 随机打乱
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= min(n, 10ll); j++){
            ans = max(ans, sqrt((dis[j].x - a[i].x) * (dis[j].x - a[i].x) + (dis[j].y - a[i].y) * (dis[j].y - a[i].y)));
        }
    }
    return ans;
}

inline void SA(){
    dl C = max(I, Maxy - Miny) * max(I, Maxx - Minx);
    x = ansx, y = ansy; // 初始化 x, y
    while(((double)clock() / CLOCKS_PER_SEC) < 0.98 && C > eps){
        dl x_ = x + rng(C), y_ = y + rng(C);
        if(x_ < Minx || y_ < Miny || x_ > Maxx || y_ > Maxy){ // 随机出的点超出范围了,没必要算
            C *= D;
            continue;
        }
        dl ans_ = calc(x_, y_);
        if(ans_ > ans){ // 更优就接受
            ans = ans_;
            ansx = x_, ansy = y_;
        }
        if(ans_ > now){ // 更优就接受
            x = x_, y = y_;
            now = ans_;
        }
        else if(gen() <= exp(1.0 * (now - ans_) / C) * gen.max()){ // 概率接受
            x = x_, y = y_;
            now = ans_;
        }
        C *= D;
    }
}

int main(){
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n;
    for(int i = 1, x_, y_; i <= n; i++){
        cin >> x_ >> y_;
        a[i] = {x_, y_};
        ansx += x_, ansy += y_;
        Minx = min(Minx, a[i].x); // 计算点的范围
        Maxx = max(Maxx, a[i].x);
        Miny = min(Miny, a[i].y);
        Maxy = max(Maxy, a[i].y);
    }
    ansx /= (1.0 * n);
    ansy /= (1.0 * n);
    ans = now = calc(ansx, ansy); // 先对所有点的平均点算一遍答案,增加正确性,以及初始化 ansx, ansy, x, y。
    while(((double)clock() / CLOCKS_PER_SEC) < 0.98){
        SA();
    }
    cout << (ll)(ans * ans + 0.5); // + 0.5 是因为强转 long long 会向下取整
    return 0;
}