[CSP-S]2026年模拟赛-2

· · 个人记录

信号基站 (station)

给定数轴上若干关键点,你可以放置 k 个基站在任意位置,每个位置能覆盖左右 s_i 距离,其中 s_i 由你决定。

求覆盖所有关键点的最小 2\sum s_i 值,k\le n\le 5\times 10^5。

理解为每个基站覆盖 [l_i, r_i] 其中 l_i, r_i 由你决定,最小化覆盖区间总和。

考虑先在每个关键点都放一个长度为 0 的基站,再考虑吞掉 n - k 个区间。

对所有点按照坐标排序,算得 n - 1 个区间,按照区间长度从小到大排序,取 \sum_{i = 1}^{n - k} len_i 为答案。

做了 15 min,没有压力。

:::success[展示代码]

#include<bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 2e5 + 5;
int n, k, a[N], ans;
signed main() {
    freopen("station.in", "r", stdin);
    freopen("station.out", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + n + 1);
    for (int i = 1; i < n; ++i) a[i] = a[i + 1] - a[i];
    sort(a + 1, a + n);
    for (int i = 1; i <= n - k; ++i) ans += a[i];
    cout << ans;
    return 0;
}

:::

轮转相遇 (rotation)

给定 n 个点的多层 DAG,i 层只向 i + 1 层连边,前 h - 1 层 每点都有 k 个出点,最后一层是终点。

机器人有指针 p,每次从当前点向第 p 出点移动,再让 p = (p + 1) \bmod k。如果在终点就不动。

求最早在整秒相遇的时刻,不存在返回 $-1$。 $n, q\le 2\times 10^5, k\le 8, nk\le 4\times 10^5,~a,b\le 10^9$。 发现如果 $h_x = h_y$ 才可能相遇。 考虑找到一整秒使得层数相等,也就是 $h_x + a t = h_y + b t$,解得 $t = \frac{h_x - h_y}{b - a}$。 发现如果 $t$ 不是整数就不能中途追上,同时此时也发现 $a = b$ 需要特殊处理。 计算两人到达终点的时间 $tx = \lceil\frac{H - h_x}{a}\rceil,ty = \lceil\frac{H - h_y}{b}\rceil$。 大概懂了,分讨 中途追上 和 最后追上。所以我们需要能快速求任意 $(x, p)$ 机器人跑任意次到达的状态。 不难想到用倍增 ST 表处理,$f_{x, p, i}$ 表示机器人在 $x$ 并且状态为 $p$ 跑 $2^i$ 次后的位置,不给状态因为可以直接推:$p' = (p + 2^i)\bmod k$。 简单实现一下发现样例都过了,想着以后来打对拍就走了,此时 $9:17$。 但是最后也没对拍,然后挂了 $20$ 分,因为 $a = b$ 部分的实现细节有问题。大样例过分水 也要承担 $40\%$ 责任。 :::info[赛时代码] ```cpp #include<bits/stdc++.h> using namespace std; constexpr int N = 2e5 + 5; int n, k, q, h[N], H, g[N][10]; int f[N][10][20]; vector<int> v[10]; int jzp(int x, int p, int c) { for (int i = 18; i >= 0; --i) if (c & (1 << i)) x = f[x][p][i], p = (p + (1 << i)) % k; return x; } signed main() { freopen("rotation.in", "r", stdin); freopen("rotation.out", "w", stdout); cin >> n >> k >> q; for (int i = 1; i <= n; ++i) cin >> h[i], ++h[i], H = max(H, h[i]), v[h[i]].push_back(i); for (int i = 1; i <= n; ++i) for (int j = 0; j < k; ++j) cin >> g[i][j]; for (int o = H; o >= 1; --o) for (int i : v[o]) if (o == H) for (int p = 0; p < k; ++p) for (int j = 0; j <= 18; ++j) f[i][p][j] = i; else for (int p = 0; p < k; ++p) { f[i][p][0] = g[i][p]; for (int j = 1; j <= 18; ++j) f[i][p][j] = f[f[i][p][j - 1]][(p + (1 << (j - 1))) % k][j - 1]; } for (int o = 1, x, y, a, b; o <= q; ++o) { cin >> x >> y >> a >> b; int tx = ceil(1.0 * (H - h[x]) / a), ty = ceil(1.0 * (H - h[y]) / b), t; // 中途相遇 if (a == b) { if (h[x] == h[y]) { t = tx; if (jzp(x, 0, a * t) == jzp(y, 0, b * t)) { int l = 0, r = t; while (l < r) { int mid = l + r >> 1; if (jzp(x, 0, a * mid) == jzp(y, 0, b * mid)) r = mid; else l = mid + 1; } cout << l << '\n'; continue; } } } else { t = (h[x] - h[y]) / (b - a); if (t * (b - a) == h[x] - h[y] and t >= 0 and t <= tx and t <= ty and jzp(x, 0, a * t) == jzp(y, 0, b * t)) { cout << t << '\n'; continue; } } //最后相遇 if (jzp(x, 0, a * tx) == jzp(y, 0, b * ty)) { cout << max(tx, ty) << '\n'; continue; } cout << "-1\n"; } return 0; } ``` ::: ## 湖泊 (lake) 给定无限大的平面和 $n$ 次操作。每次将一方格染黑,已经染黑过则不操作。 每次操作后询问 被黑色围住的白色连通块个数,**四**连通,$n\le 2\times 10^5$。 [考后] 考时读错题了,以为白色连通块要八连通,所以下面的考场思路是按照八连通做的。 先进行八连通离散化,去写了十分钟就写完了。 然后注意到可以离线然后倒着来,看成每次删去一个黑点,求被包住的白色连通块数量。 发现被黑色包住的白色连通块数量就是所有白色连通块数量再减 $1$,用并查集维护即可。 写了一下发现过不了小样例,发现可以重复染黑一个格子,于是可以记录记录格子被染黑次数,倒序处理时每次将值减 $1$,如果变成 $0$ 就当删除。 发现能过所有数据,想着以后回来对拍就走了,此时大概 $10:15$。 $12:11$ 开始再检查本题,花 $3$ min 打了个暴力和对拍,发现炸了!但是我心态好,冷静调试,在 $12:15$ 通过对拍。 [考后] 挂成 $8$ pts,把代码里 $8$ 改成 $4$ 就可过!问题是如果你理解为八连通是可以解释小样例并且通过所有大样例的,大样例过分水承担 $80\%$ 责任。 :::success[通过代码] ```cpp #include<bits/stdc++.h> using namespace std; constexpr int N = 6e5 + 5; int n, b[N], sbx, sby, tot, ans, out[N]; #define P(i, j) (i * sby + j) unordered_map<int, int> mp, cnt, fa; struct node { int x, y; } a[N]; int ask(int x) { if (x == fa[x]) return x; return fa[x] = ask(fa[x]); } void meg(int x, int y) { x = ask(x), y = ask(y); if (x != y) fa[x] = y, --ans; } int fx[] = {0, 0, 1, -1}; int fy[] = {1, -1, 0, 0}; signed main() { freopen("lake.in", "r", stdin); freopen("lake.out", "w", stdout); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y, b[++tot] = a[i].x, b[++tot] = a[i].x - 1, b[++tot] = a[i].x + 1; sort(b + 1, b + tot + 1); for (int i = 1; i <= tot; ++i) if (i == 1 or b[i] != b[i - 1]) mp[b[i]] = ++sbx; for (int i = 1; i <= n; ++i) a[i].x = mp[a[i].x]; mp.clear(), tot = 0; for (int i = 1; i <= n; ++i) b[++tot] = a[i].y, b[++tot] = a[i].y - 1, b[++tot] = a[i].y + 1; sort(b + 1, b + tot + 1); for (int i = 1; i <= tot; ++i) if (i == 1 or b[i] != b[i - 1]) mp[b[i]] = ++sby; for (int i = 1; i <= n; ++i) a[i].y = mp[a[i].y]; for (int i = 1; i <= n; ++i) ++cnt[P(a[i].x, a[i].y)]; for (int i = 1; i <= sbx; ++i) for (int j = 1; j <= sby; ++j) if (!cnt[P(i, j)]) fa[P(i, j)] = P(i, j), ++ans; for (int i = 1; i <= sbx; ++i) for (int j = 1; j <= sby; ++j) if (!cnt[P(i, j)]) for (int k = 0; k < 4; ++k) { int nx = i + fx[k], ny = j + fy[k]; if (nx < 1 or ny < 1 or nx > sbx or ny > sby or cnt[P(nx, ny)]) continue; meg(P(i, j), P(nx, ny)); } for (int i = n, x, y; i >= 1; --i) { out[i] = ans - 1; x = a[i].x, y = a[i].y, --cnt[P(x, y)]; if (cnt[P(x, y)]) continue; fa[P(x, y)] = P(x, y), ++ans; for (int k = 0; k < 4; ++k) { int nx = x + fx[k], ny = y + fy[k]; if (nx < 1 or ny < 1 or nx > sbx or ny > sby or cnt[P(nx, ny)]) continue; meg(P(x, y), P(nx, ny)); } } for (int i = 1; i <= n; ++i) cout << out[i] << '\n'; return 0; } ``` ::: ## 高原远征 (plateau) 给定一颗树,每个节点有 $a_i, d_i$,进入节点 $i$ 时要求体力值 $\ge a_i$,且进入后体力值 $E'$ 变为 $\min(C, E + d_i)$,其中 C 是本次远征的体力上限。 一次远征从 $x$ 到 $y$,最多可以使用一次补给,在到达某营地前使用补给,让体力值变为体力上限。 多次询问,每次给定一个远征目标 $x\to y$ 和初始体力 $E$,询问最小可能的 $C$,询问 $C$ 最小的前提下最晚可能补给位置(不补给视为绝对最晚,输出0),询问此方案的到达时间。 先考虑 $d_i > 0$ 的情况,考虑到 $E$ 只会变大,发现第一个答案一定是路径最大值,第三个答案一定是路径最大值,考虑第二个答案。 直接从 $x$ 到 $y$ 枚举路径上的每一个点 $i$,如果当前 $E < a_i$ 那就使用机会,那么第二个答案就是 $i$;如果 $E$ 达到 $a$ 路径最大值,那么第二个答案就是 $0$。 轻松拿到 $20$ pts,考虑 $n, q\le 2\times 10^3$ 的情况。 比较类似,二分答案,在 check 内部模拟,在需要补给时补给,如果需要两次补给就不合法,其他部分很好做了。 :::info[赛时代码] ```cpp #include<bits/stdc++.h> using namespace std; #define int long long constexpr int N = 2e5 + 5; int n, q, a[N], d[N]; vector<int> v[N]; int f[N][20], w[N][20], dep[N]; void pdfs(int x, int fa) { f[x][0] = fa, w[x][0] = a[x], dep[x] = dep[fa] + 1; for (int i = 1; i <= 18; ++i) f[x][i] = f[f[x][i - 1]][i - 1], w[x][i] = max(w[x][i - 1], w[f[x][i - 1]][i - 1]); for (int y : v[x]) if (y != fa) pdfs(y, x); } int lby; int lca(int x, int y) { // 顺便把路径最大值喂给 lby 帮我存 lby = 0; if (dep[x] < dep[y]) swap(x, y); for (int i = 18; i >= 0; --i) if (dep[f[x][i]] >= dep[y]) lby = max(lby, w[x][i]), x = f[x][i]; if (x == y) { lby = max(lby, w[x][0]); return x; } for (int i = 18; i >= 0; --i) if (f[x][i] != f[y][i]) lby = max({lby, w[x][i], w[y][i]}), x = f[x][i], y = f[y][i]; lby = max({lby, w[x][1], w[y][0]}); return f[x][0]; } int jzp(int x, int c) { for (int i = 18; i >= 0; --i) if (c & (1 << i)) x = f[x][i]; return x; } int len, id[N]; vector<int> tmp; struct RET { bool chk; int lst, ed; }; RET check(int now, int C) { RET ret = {1, 0, 0}; bool use = 0; for (int o = 1; o <= len; ++o) { int i = id[o]; if (C < a[i]) return {0, 0, 0}; if (now < a[i]) { if (use) return {0, 0, 0}; use = 1, ret.lst = i, now = C; } now = min(C, now + d[i]); } ret.ed = now; return ret; } signed main() { freopen("plateau.in", "r", stdin); freopen("plateau.out", "w", stdout); cin >> n >> q; for (int i = 1; i <= n; ++i) cin >> a[i] >> d[i]; for (int i = 1, x, y; i < n; ++i) cin >> x >> y, v[x].push_back(y), v[y].push_back(x); if (n <= 2000 and q <= 2000) { // 20 pts pdfs(1, 0); for (int i = 1, E, x, y, l, r, lc; i <= q; ++i) { cin >> x >> y >> E, len = 0, l = E, r = 1e16, lc = lca(x, y); while (x != lc) id[++len] = x, x = f[x][0]; id[++len] = lc, tmp.clear(); while (y != lc) tmp.push_back(y), y = f[y][0]; reverse(tmp.begin(), tmp.end()); for (int o : tmp) id[++len] = o; while (l < r) { int mid = l + r >> 1; if (check(E, mid).chk) r = mid; else l = mid + 1; } RET t = check(E, l); cout << l << ' ' << t.lst << ' ' << t.ed << '\n'; } } else { // 20 pts pdfs(1, 0); for (int i = 1, E, x, y, lc; i <= q; ++i) { cin >> x >> y >> E, lc = lca(x, y); if (E >= lby) { cout << E << " 0 " << E << '\n'; continue; } cout << lby << ' '; bool ot = 0; while (x != lc) { if (E < a[x]) { ot = 1, cout << x << ' '; break; } E = min(lby, E + d[x]); if (E == lby) { ot = 1, cout << "0 "; break; } x = f[x][0]; } if (ot) { cout << lby << '\n'; continue; } for (int j = dep[y] - dep[lc]; j >= 0; --j) { x = jzp(y, j); if (E < a[x]) { ot = 1, cout << x << ' '; break; } E = min(lby, E + d[x]); if (E == lby) { ot = 1, cout << "0 "; break; } } if (ot) { cout << lby << '\n'; continue; } else cout << "\n垃圾模拟赛,不用认真打! -LBY \n"; } } return 0; } ``` :::