题解:P6900 [ICPC 2014 WF] Sensor Network
各位大佬都用了非常好的确定性的计算几何方法!!!
蒟蒻就只能来一篇随机化题解了。(题解区的其它随机化题解好像都没分析正确率捏)
问题转换
考虑将可以直接通信的点对之间连边,原题转化为求一张图的最大团。
最大团的随机化算法
过程如下:
- 生成一个
1 \sim n 的排列,并将其随机打乱。 - 维护集合
S 表示当前求出的最大团。遍历排列,若当前点与S 内所有点都有直接连边,则向S 中加入该点。否则什么也不做。 - 重复上述过程合适次数,并更新最优解。
正确率分析
考虑最坏情况下这张图会长什么样:
是的,三元环套菊花。
此时,求出的解正确当且仅当排列的前两个点在
在本题的数据范围与时间限制下,
代码
如果不喜欢卡时的话可以把 while (curtime() - bg < 1950) 一句改为 for (int _ = 0; _ < SuitableTimes; ++_)。SuitableTimes 可以自己选一个合适的数。实测填
码风有点丑,请见谅。
#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;
}