揭秘“You have no eggs!”背后的故事以及论如何在一小时内速通 CSPS 2025 初赛
原来一切都是一场梦,包括那场暴雨,包括我遇见你,包括我学过 OI。
啊……你是被标题的前半段吸引来的?不妨拉到阅读程序第二题的部分吧。有有趣的视频和图图的!
upd on
截至
CABDD
DAADD
CAACB
TTFADC
TFTBCA
FTTBDD
ABBCC
BBDAB
题面来自于 https://docs.qq.com/pdf/DUW5YSFpzc1pWR3Ba?_t=1758420953224&nlc=1&u=f38f4ad33172474692ce8a1bdc7ca911
第一部分 单项选择题
1
有
答案:
解:插板法即可。
2
在 KMP 算法中,对于模式串
答案:
解:模拟即可。
3
对一个大小为
答案:
解:模拟即可。
4
将字符串 cat、car、cart、case、dog、do 插入一个空的 Trie 树中。构建玩 Trie 树(包括根节点)共有多少个结点?
答案:
解:模拟即可。
5
对于一个包含
解:稍微了解一下这个玩意目前为止只有指数级做法,考场上也可以随便构造一张图来举反例。
6
在一个大小为
答案:
解:模拟即可。
7
一个包含
答案:
解:将顶点从小到大串起来即可。
8
如果一颗二叉搜索树的后序遍历序列是
答案:
解:根据后序遍历的定义,取最后一个树作为根,然后根据二叉搜索树左边比根小右边比根大的特点,找到树的左右两部分,最后递归建树即可。
9
一个 01 背包问题,背包容量为
答案:
解:注意到有
充分发扬人类智慧,剩下那一个物品不太可能被装进背包里,并取到答案
当然,如果你喜欢你也可以像我一样在用一个小时速通后用剩下那一小时手算动态规划数组。
10
在一棵以结点
答案:
解:显然一个结点与根的 LCA 仍然是根。一眼盯真即可。
11
递推关系式
答案:
解:未经思考可能会按直觉直接选 不会不想用主定理,遂有如下观察:
数形结合即可。
12
在一个初始为空的最小堆(min-heap)中,依次插入元素
答案:
解:注意到最小堆一定要最小。充分发扬人类智慧,我们不需要用笔在纸上手算建堆,只需要将最小值拎出来操作即可。因此照题意删除查询即可。
好久没有看到“最小堆”这样的表述了,以前别人都说是“小根堆”,好怀念“最小堆”这种说法啊……
13
答案:
解:典。直接菜鱼校徽容斥即可。答案即为
14
斐波那契数列的定义为
解:一眼盯真即可。
15
有
解:笔者在考场上曾试图推理贪心策略,奈何笔者退役太久实力不够没有推出来。希望有读者能够给出来,了却笔者的遗愿。
充分发扬人类智慧,由于任务只有
存在方案可行,故以
终究还是没能改变既定的结局吗,终究还是因为我不够努力吗
第二部分 阅读程序
第一题
#include <algorithm>
#include <cstdio>
#include <cstring>
bool flag[27];
int n;
int p[27];
int ans=0;
void dfs(int k){
if (k == n + 1){
++ ans;
return;
}
for (int i = 1;i <= n; ++i){
if (flag[i]) continue;
if (k > 1 && i == p[k - 1] + 1)continue;
p[k] = i;
flag[i] = true;
dfs(k + 1);
flag[i] = false;
}
return;
}
int main(){
scanf("%d",&n);
dfs(1);
printf("%d\n",ans);
return 0;
}
这个阅读程序较为简单。可以看出一个 dfs 函数是在生成一个排列,且不允许有两个
判断题
16(1 分)
当输入的
解:暴力枚举即可。
1 3 2
2 1 3
3 2 1
17
在 dfs 函数运行过程中,
解:一眼盯真即可。
18
删除第 flag[i]=false;,对答案不会有任何影响。
解:一眼盯真即可。
选择题
19
当输入的
答案:
解:充分发扬人类智慧,由于
1 3 2 4
1 4 3 2
2 1 4 3
2 4 1 3
2 4 3 1
3 1 4 2
3 2 1 4
3 2 4 1
4 1 3 2
4 2 1 3
4 3 2 1
20
如果因为某些问题,导致程序运行第 dfs 函数之前,数组
解:一眼盯真即可。
21
加入删去第 if(flag[i])continue;,输入
答案:
解:注意到删去该语句后
1 1 1
1 1 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 3
2 2 1
2 2 2
3 1 1
3 1 3
3 2 1
3 2 2
3 3 1
3 3 2
3 3 3
第二题
#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
int cnt_broken = 0;
int cnt_check = 0;
int n,k;
inline bool check(int h){
printf("now check:%d\n",h);
++cnt_check;
if (cnt_broken == 2){
printf("You have no egg!\n");
return false;
}
if (h >= k){
++cnt_broken;
return true;
} else {
return false;
}
}
inline bool assert_ans(int h){
if(h == k) {
printf("You are Right using %d checks\n",cnt_check);
return true;
} else {
printf("Wrong answer!\n");
return false;
}
}
inline void guess1(int n){
for(int i = 1;i <= n; ++i) {
if (check(i)) {
assert_ans(i);
return;
}
}
}
inline void guess2(int n){
int w = 0;
for(w = 1;w * (w + 1) / 2 < n; ++w)
;
for (int ti = w,nh = w;; --ti,nh += ti,nh = std::min(nh,n)) {
if (check(nh)) {
for (int j = nh - ti + 1; j < nh; ++j){
if (check(j)) {
assert_ans(j);
return;
}
}
assert_ans(nh);
return;
}
}
}
int main(){
scanf("%d%d", &n, &k);
int t;
scanf("%d", &t);
if (t == 1){
guess1(n);
} else {
guess2(n);
}
return 0;
}
这个阅读程序其实很有意思。笔者在看到 You have no eggs! 时马上想歪了,以为是出题者的恶趣味,并且想起了某到 UVA 状压题的神人题面,上面的松鼠有两个大 OO 。然后就把这一题放到了最后来做。
意识到 You have no eggs! 其实并不是出题人的恶趣味,反倒是一种提示呢。
就像视频里提到的那样,现在有一栋楼,有个最高安全楼层使得蛋从该楼层上扔下来恰好不会摔碎。你的任务是在只有两个蛋可以拿来试验的情况下确定最高安全楼层是哪个。
视频里提供了一个基于分块的思路,即将先楼层分成长度严格单调递减的
值得一提的是这道题还有个加强版,把蛋的数量从两个加到了
话说怎么洛谷讨论区全都是都是 已严肃完成今日"You have no egg!"大学习 呀,还有人发了张很难崩的图……突然感觉这个问题失去了她原本的意义了呀 {{{(>_<)}}}
啊好了不多说了接下来是题面>﹏<
(注意:下述的“猜测数”为调用 check 函数的次数(即 cnt_check 的值);“猜测正确”的含义为 asser_ans 函数 return true;(执行第
判断题
22
当输入为 6 5 1 的时,猜测次数为 6 5 2 的时,猜测次数为
解:按题意模拟即可。
23
不管输入的
解:笔者考场上没经过脑子就直接选了正确……后来想想其实时可以随便构造一个……但笔者码字的时候是半夜两点旁边又有家长虎视眈眈所以懒得构造了!
24
不管
解:一眼盯真即可。
选择题
25
函数 guess1 在运行过程中,cnt_broken 的值最多为()。
答案:
解:笔者考场上看成是循环次数了……痛失若干分……实际上因为是从下往上暴力枚举的所以蛋第一次碎掉时就已经知道答案了……
26
函数 guess1 在运行过程中,最多使用的猜测次数的量级为()。
答案:
解:一眼盯真即可。
27
当输入的
答案:
解:首先排除
第三题
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define ll long long
int n, m;
std::vector<int> k,p;
inline int mpow(int x, int k) {
int ans = 1;
for (; k; k = k >> 1, x = x * x) {
if(k & 1)
ans = ans* x;
}
return ans;
}
std::vector<int> ans1, ans2;
int cnt1, cnt2;
inline void dfs(std::vector<int>& ans, int& cnt, int l, int r,int v){
if (l > r) {
++cnt;
ans.push_back(v);
return;
}
for (int i = 1; i <= m; ++i) {
dfs(ans, cnt, l + 1, r, v + k[l] * mpow(i, p[l]));
}
return;
}
std::vector<int> cntans1;
int main(){
scanf("%d%d", &n, &m);
k.resize(n + 1);
p.resize(n + 1);
for (int i = 1; i <= n; ++i){
scanf("%d%d", &k[i], &p[i]);
}
dfs(ans1, cnt1, 1, n >> 1, 0);
dfs(ans2, cnt2, (n >> 1) + 1, n, 0);
std::sort(ans1.begin(), ans1.end());
int newcnt1 = 1;
cntans1.push_back(1);
for (int i = 1; i < cnt1; ++i){
if (ans1[i] == ans1[newcnt1 - 1]) {
++cntans1[newcnt1 - 1];
} else {
cntans1[newcnt1++] = ans1[i];
cntans1.push_back(1);
}
}
cnt1 = newcnt1;
std::sort(ans2.begin(),ans2.end());
int las = 0;
ll ans = 0;
for (int i = cnt2 - 1; i >= 0; --i){
for(; las < cnt1 && ans1[las] + ans2[i] < 0; ++las)
;
if(las < cnt1 && ans1[las] + ans2[i] == 0)
ans += cntans1[las];
}
printf("%lld\n",ans);
return 0;
}
这题也相对来说比较简单……?dfs 函数大致在干什么是可以看出来的,中间的手搓离散化与去重也很简单,后面的双指针部分也很容易。笔者一开始研究了好久……结果看到最后一个选择题的选项就豁然开朗了。出题人直接把答案放嘴边可还行。
判断题
28
删除第 51 行的 std::sort(ans2.begin(),ans2.end()); 后,代码输出的结果不会受到影响。
解:显然把排序去掉会影响双指针的……
29
假设计算过程中不发生溢出,函数 mpow(x,k) 的功能是求出
解:这是一份很经典也很简单的龟速乘快速幂的代码,原理是将指数二进制分解。如果是第一次见可能会有点困难?
30
代码中第
解:这个比较显然,一眼盯真即可。
选择题
31
当输入为 3 15 1 2 -1 2 1 2 时,输出结果为()
答案:
解:由
将 3 4 5 与 5 12 13。将 3 4 5 放缩可得 6 8 10 与 9 12 15。把
5 3 4
10 6 8
15 9 12
13 5 12
5 4 3
10 8 6
15 12 9
13 12 5
32
记程序结束前
答案:
解:首先排除 dfs 函数里填数的时候使用了龟速乘,所以时间复杂度除了排序还要加上快速幂。故选
33
本题所求出的是()
解:观察代码可排除 dfs 函数可得填数的时候不能取到
啊啊……原来还不是因为我不够努力啊……自那天起,就再也不可能改变既定的命运了。
第三部分 单项选择题
第一题
(特殊最短路)给定一个含
以下代码求解了上述问题。试补全程序。
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const long long INF = 1e18;
struct Edge {
int to;
int weight;
};
struct State {
long long dist;
int u;
bool used_freebie; // 0 for not used, 1 for used
bool operator<(const State &other) const {
return dist > other.dist;
}
};
int main() {
int n, m, s, t;
cin >> n >> m >> s >> t;
vector<vector<Edge>> adj(n + 1);
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
vector<vector<long long>> d(n + 1, vector<long long>(2, INF));
priority_queue<State, vector<State>, greater<State>> pq;
d[s][0] = 0;
pq.push({0, s, ___①___});
while (!pq.empty()) {
State current = pq.top();
pq.pop();
long long dist = current.dist;
int u = current.u;
int used = current.used_freebie;
if (dist > ___②___) {
continue;
}
for (const auto &edge : adj[u]) {
int v = edge.to;
int w = edge.weight;
if (d[u][used] + w < ___③___) {
___③___ = d[u][used] + w;
pq.push({d[v][used], v, used});
}
if (used == 0) {
if (___④___ < d[v][1]) {
d[v][1] = ___④___;
pq.push({d[v][1], v, 1});
}
}
}
}
cout << ___⑤___ <<endl;
return 0;
}
这个题很简单,显然
34
①处应填()
35
②处应填()
36
③处应填()
37
④处应填()
38
⑤处应填()
第二题
工厂打算通过客户反馈来间接测试生产线,从而找到存在缺陷的生产线。工厂有
每一轮测试为,从若干生产线的产品取样混合成一个批次发给客户。若该批次中包含缺陷生产线的产品,客户将要求退货(结果记为
受售后压力限制,在所有发货批次中,最多只能有
以下程序实现了工厂的目标,包含两部分:
i) 确定
ii) 根据测试结果推断存在缺陷的生产线。
该程序确定
test_subset() 函数为抽象测试接口,输入所有批次的方案并返回一个二进制编码;该编码表示为每批次的检测结果(即最低位是第
试补全程序。
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
long long comb(int w, int i) {
if (i < 0 || i > w) {
return 0;
}
long long res = 1;
for (int t = 1; t <= i; ++t) {
res = res * (w - t + 1) / t;
}
return res;
}
// 计算长度为 w、1 的个数 ≤ k 的码字总数
long long count_patterns(int w, int k) {
long long total = 0;
for (int t = 0; t <= min(w, k); ++t) {
total += comb(w, t);
}
return total;
}
// 抽象测试接口
int test_subset(const vector<vector<int>>& plan);
int solve(int n, int k) {
// === 第 1 步:求最小 w ===
int w = 1;
while (___①___) {
++w;
}
cout << w << endl;
// === 第 2 步:生成测试方案 ===
vector<vector<int>> code(n, vector<int>(w, 0));
int idx = 0;
for (int ones = 0; ones <= k && idx < n; ++ones) {
vector<int> bits(w, 0);
fill(bits.begin(), bits.begin() + ones, 1);
do {
for (int b = 0; b < w; ++b){
code[idx][b] = bits[b];
}
++idx;
if(idx >= n) {
break;
}
}while (std::___②___);
vector<vector<int>> plan(w);
for (int i = 0; i < w; ++i) {
for (int j = 0; j < n; ++j) {
if (___③___) {
plan[i].push_back(j);
}
}
}
// === 第 3 步:调用测试接口 ===
int signature = test_subset(plan);
// === 第 4 步:结果解码 ===
vector<int> sig_bits(w, 0);
for (int i = 0; i < w; ++i) {
if (___④___) {
sig_bits[i]=1;
}
}
for (int j = 0; j < n; ++j) {
if (___⑤___) return j;
}
}
int main() {
int n,k;
cin >> n >> k;
int ans = solve();//笔者注:这里本来应该传入 n 和 k 的,但不知道为什么考场试卷上没有
cout << ans << endl;
return 0;
}
笔者在考场上没有怎么看懂题面,更没有看懂 comb 和 count_patterns 在干什么。因此笔者当时是从语法的角度来做这题的!也有其他人做了很好的解析,这里我不想再赘述了。
说实话,到这里为止,专栏的内容到挺符合“速通”,但笔者缺没有认认真真地详解每道题……特别是这道题。而且专栏还有许多不堪入目的错误连改都没改就发出来了……所以,很抱歉浪费您的时间。对不起。
39
①处应填()
40
②处应填()
41
③处应填()
42
④处应填()
43
⑤处应填()
终于结束了。
笔者不会用 AI,也没有 AI 账号,所以整篇专栏除了完善程序一、二的代码全是对着图片手打的,如果有错误的地方请指出来。由于笔者已经退役,可能无法及时更新专栏,抱歉。
如果我当时再多做一点,结局会不会有所不同呢?