跳到正文

题解

题解:P6900 [ICPC 2014 WF] Sensor Network

_cbw约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年2月13日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 5 分钟
获赞
1
题目
P6900

版本

  1. v1当前

各位大佬都用了非常好的确定性的计算几何方法!!!

蒟蒻就只能来一篇随机化题解了。(题解区的其它随机化题解好像都没分析正确率捏)

问题转换

考虑将可以直接通信的点对之间连边,原题转化为求一张图的最大团。

最大团的随机化算法

过程如下:

  1. 生成一个 1∼n1 \sim n 的排列,并将其随机打乱。
  2. 维护集合 SS 表示当前求出的最大团。遍历排列,若当前点与 SS 内所有点都有直接连边,则向 SS 中加入该点。否则什么也不做。
  3. 重复上述过程合适次数,并更新最优解。

正确率分析

考虑最坏情况下这张图会长什么样:

三元环套菊花

是的,三元环套菊花。

此时,求出的解正确当且仅当排列的前两个点在 1∼31 \sim 3 之间。第一个点符合要求的概率为 3n\frac{3}{n},在此条件下第二个点符合要求的概率为 2n−1\frac{2}{n - 1}。因此,随机一次的正确率为 6n(n−1)\frac{6}{n(n - 1)}。由上,随机 tt 次的正确率为 1−[1−6n(n−1)]t1 - \left[1 - \frac{6}{n(n - 1)} \right]^t。

在本题的数据范围与时间限制下,tt 至少可以为 2×1052 \times 10^5,相应的正确率约为 1−2.199308347×10−531 - 2.199308347\times10^{-53},是绰绰有余的。

代码

如果不喜欢卡时的话可以把 while (curtime() - bg < 1950) 一句改为 for (int _ = 0; _ < SuitableTimes; ++_)。SuitableTimes 可以自己选一个合适的数。实测填 300000300000 能过,最慢 1.39s1.39\mathrm{s}。

码风有点丑,请见谅。

C++
#include <algorithm>
#include <chrono>
#include <iostream>
#include <numeric>
#include <random>
#define MAXN 103
#define curtime() (chrono::duration_cast<chrono::milliseconds>(chrono::steady_clock::now().time_since_epoch()).count())
using namespace std;

inline int pow2(const int x)
{
    return x * x;
}

int main()
{
    struct
    {
        int x, y;
    } pos[MAXN];
    int ans = 0, vecp, vec[MAXN], a[MAXN], ansa[MAXN];
    bool ok[MAXN][MAXN];

    auto bg = curtime();
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int n, d;
    cin >> n >> d;
    for (int i = 1; i <= n; ++i)
        cin >> pos[i].x >> pos[i].y;
    d *= d;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
            ok[i][j] = (pow2(pos[i].x - pos[j].x) + pow2(pos[i].y - pos[j].y) <= d);
    mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
    iota(a + 1, a + n + 1, 1);
    bool flag;
    while (curtime() - bg < 1950)
    {
        shuffle(a + 1, a + n + 1, rng);
        vecp = 0;
        for (int i = 1; i <= n; ++i)
        {
            flag = true;
            for (int j = 1; j <= vecp; ++j)
            {
                if (!ok[a[i]][vec[j]])
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
                vec[++vecp] = a[i];
        }
        if (vecp > ans)
        {
            ans = vecp;
            copy(vec + 1, vec + vecp + 1, ansa + 1);
        }
    }
    cout << ans << '\n';
    for (int i = 1; i <= ans; ++i)
        cout << ansa[i] << ' ';
    return 0;
}
原文
存档评论1 条抓取于