跳到正文

个人记录

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

Believe_in_dreams约 19 分钟
原文
文章信息与版本

文章信息

发布
2026年8月19日
收录
2026年8月19日
更新
2026年11月15日
篇幅
约 19 分钟

版本

  1. v1当前

信号基站 (station)

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

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

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

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

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

做了 1515 min,没有压力。

展示代码
C++
#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)

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

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

qq 次给定 x,y,a,bx, y, a, b,两个机器人从 x,yx, y 出发。每秒 xx 移动 aa 步,yy 移动 bb 步。

求最早在整秒相遇的时刻,不存在返回 −1-1。

n,q≤2×105,k≤8,nk≤4×105, a,b≤109n, q\le 2\times 10^5, k\le 8, nk\le 4\times 10^5,~a,b\le 10^9。

发现如果 hx=hyh_x = h_y 才可能相遇。

考虑找到一整秒使得层数相等,也就是 hx+at=hy+bth_x + a t = h_y + b t,解得 t=hx−hyb−at = \frac{h_x - h_y}{b - a}。

发现如果 tt 不是整数就不能中途追上,同时此时也发现 a=ba = b 需要特殊处理。

计算两人到达终点的时间 tx=⌈H−hxa⌉,ty=⌈H−hyb⌉tx = \lceil\frac{H - h_x}{a}\rceil,ty = \lceil\frac{H - h_y}{b}\rceil。

大概懂了,分讨 中途追上 和 最后追上。所以我们需要能快速求任意 (x,p)(x, p) 机器人跑任意次到达的状态。

不难想到用倍增 ST 表处理,fx,p,if_{x, p, i} 表示机器人在 xx 并且状态为 pp 跑 2i2^i 次后的位置,不给状态因为可以直接推:p′=(p+2i) mod kp' = (p + 2^i)\bmod k。

简单实现一下发现样例都过了,想着以后来打对拍就走了,此时 9:179:17。

但是最后也没对拍,然后挂了 2020 分,因为 a=ba = b 部分的实现细节有问题。大样例过分水 也要承担 40%40\% 责任。

赛时代码
C++
#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)

给定无限大的平面和 nn 次操作。每次将一方格染黑,已经染黑过则不操作。

每次操作后询问 被黑色围住的白色连通块个数,四连通,n≤2×105n\le 2\times 10^5。

[考后] 考时读错题了,以为白色连通块要八连通,所以下面的考场思路是按照八连通做的。

先进行八连通离散化,去写了十分钟就写完了。

然后注意到可以离线然后倒着来,看成每次删去一个黑点,求被包住的白色连通块数量。

发现被黑色包住的白色连通块数量就是所有白色连通块数量再减 11,用并查集维护即可。

写了一下发现过不了小样例,发现可以重复染黑一个格子,于是可以记录记录格子被染黑次数,倒序处理时每次将值减 11,如果变成 00 就当删除。

发现能过所有数据,想着以后回来对拍就走了,此时大概 10:1510:15。

12:1112:11 开始再检查本题,花 33 min 打了个暴力和对拍,发现炸了!但是我心态好,冷静调试,在 12:1512:15 通过对拍。

[考后] 挂成 88 pts,把代码里 88 改成 44 就可过!问题是如果你理解为八连通是可以解释小样例并且通过所有大样例的,大样例过分水承担 80%80\% 责任。

通过代码
C++
#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)

给定一颗树,每个节点有 ai,dia_i, d_i,进入节点 ii 时要求体力值 ≥ai\ge a_i,且进入后体力值 E′E' 变为 min⁡(C,E+di)\min(C, E + d_i),其中 C 是本次远征的体力上限。

一次远征从 xx 到 yy,最多可以使用一次补给,在到达某营地前使用补给,让体力值变为体力上限。

多次询问,每次给定一个远征目标 x→yx\to y 和初始体力 EE,询问最小可能的 CC,询问 CC 最小的前提下最晚可能补给位置(不补给视为绝对最晚,输出0),询问此方案的到达时间。

先考虑 di>0d_i > 0 的情况,考虑到 EE 只会变大,发现第一个答案一定是路径最大值,第三个答案一定是路径最大值,考虑第二个答案。

直接从 xx 到 yy 枚举路径上的每一个点 ii,如果当前 E<aiE < a_i 那就使用机会,那么第二个答案就是 ii;如果 EE 达到 aa 路径最大值,那么第二个答案就是 00。

轻松拿到 2020 pts,考虑 n,q≤2×103n, q\le 2\times 10^3 的情况。

比较类似,二分答案,在 check 内部模拟,在需要补给时补给,如果需要两次补给就不合法,其他部分很好做了。

赛时代码
C++
#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;
}
原文