#CSPS2024CS. csps2024
csps2024
CSP-S 2024 提高级 第一轮试题(初赛)
一、单项选择题(共 15 题,每题 2 分,共计 30 分;每题有且仅有一个正确选项)
- 在 Linux 系统中,如果你想显示当前工作目录的路径,应该使用哪个命令?( ) {{ select(1) }}
- pwd
- cd
- ls
- echo
- 假设一个长度为 n 的整数数组中每个元素互不相同,且这个数组是无序的。要找到这个数组中最大元素的时间复杂度是多少?( ) {{ select(2) }}
- O(n)
- O(log n)
- O(n log n)
- O(1)
- 在 C++ 中,以下哪个函数调用会造成栈溢出?( ) {{ select(3) }}
- int foo() { return 0; }
- int bar() { int x=1; return x; }
- void baz() { int a[1000]; baz(); }
- void qux() { return; }
- 在一场比赛中,有 10 名选手参加,前三名将获得金银铜牌,若不允许并列,且每名选手只能获得一枚奖牌,则不同的颁奖方式共有多少种?( ) {{ select(4) }}
- 120
- 720
- 504
- 1000
- 下面哪个数据结构最适合实现先进先出(FIFO)的功能?( ) {{ select(5) }}
- 栈
- 队列
- 线性表
- 二叉搜索树
- 已知 f(1) = 1,且对于 n>=2 有 f(n) = f(n-1) + f(⌊n/2⌋),则 f(4) 的值为:( ) {{ select(6) }}
- 4
- 5
- 6
- 7
- 假设一个包含 n 个顶点的无向图,且该图是欧拉图。以下关于该图的描述中哪一项不一定正确?( ) {{ select(7) }}
- 所有顶点的度数均为偶数
- 该图连通
- 该图存在一个欧拉回路
- 该图的边数是奇数
- 对数组进行二分查找的过程中,以下哪个条件必须满足?( ) {{ select(8) }}
- 数组必须是有序的
- 数组必须是无序的
- 数组长度必须是 2 的幂
- 数组中的元素必须是整数
- 考虑一个自然数 n 以及一个模数 m,你需要计算 n 的逆元(即 n 在模 m 意义下的乘法逆元)。下列哪种算法最为合适?( ) {{ select(9) }}
- 使用暴力方法依次尝试
- 使用扩展欧几里得算法
- 使用快速幂算法
- 使用线性筛法
- 在设计一个哈希表时,为了减少冲突,需要使用适当的哈希函数和冲突解决策略。已知某哈希表中有 n 个键值对,表的装载因子为 α(0<α≤1)。在使用开放地址法解决冲突的过程中,最坏情况下查找一个元素的时间复杂度为( ) {{ select(10) }}
- O(1)
- O(log n)
- O(1/(1-α))
- O(n)
- 假设有一颗 h 层的完全二叉树,该树最多包含多少个节点?( ) {{ select(11) }}
- 2^h - 1
- 2^(h+1) - 1
- 2^h
- 2^(h+1)
- 设有一个 10 个顶点的完全图,每两个顶点之间都有一条边,有多少个长度为 4 的环?( ) {{ select(12) }}
- 120
- 210
- 630
- 5040
- 对于一个整数 n,定义 f(n) 为 n 的各位数字之和,问使 f(f(x)) = 10 的最小自然数 x 是多少?( ) {{ select(13) }}
- 29
- 199
- 299
- 399
- 设有一个长度为 n 的 01 字符串,其中有 k 个 1,每次操作可以交换相邻两个字符。在最坏的情况下将这 k 个 1 移到字符串最右边所需要的交换次数是多少?( ) {{ select(14) }}
- k
- k*(k-1)/2
- (n-k)*k
- (2n-k-1)*k/2
- 如图是一张包含 7 个顶点的有向图。如果要删除其中一些边,使得从节点 1 到节点 7 没有可行路径,且删除的边数最少,请问总共有多少种可行的删除边的集合?( ) {{ select(15) }}

- 1
- 2
- 3
- 4
二、阅读程序
阅读程序(1)
1 #include <iostream>
2 using namespace std;
3
4 const int N = 1000;
5 int c[N];
6
7 int logic(int x, int y) {
8 return (x & y) ^ ((x ^ y) | (~x & y));
9 }
10
11 void generate(int a, int b, int *c) {
12 for (int i = 0; i < b; i++) {
13 c[i] = logic(a, i) % (b + 1);
14 }
15 }
16
17 void recursion(int depth, int *arr, int size) {
18 if (depth <= 0 || size <= 1) return;
19 int pivot = arr[0];
20 int i = 0, j = size - 1;
21 while (i <= j) {
22 while (arr[i] < pivot) i++;
23 while (arr[j] > pivot) j--;
24 if (i <= j) {
25 int temp = arr[i];
26 arr[i] = arr[j];
27 arr[j] = temp;
28 i++; j--;
29 }
30 }
31 recursion(depth - 1, arr, j + 1);
32 recursion(depth - 1, arr + i, size - i);
33 }
34
35 int main() {
36 int a, b, d;
37 cin >> a >> b >> d;
38 generate(a, b, c);
39 recursion(d, c, b);
40 for (int i = 0; i < b; i++) cout << c[i] << " ";
41 cout << endl;
42 }
16-1. 当 1000 >= d >= b 时,输出的序列是有序的。( ) {{ select(16-1) }}
- 正确
- 错误
16-2. 当输入 "5 5 1" 时,输出为 "1 1 5 5 5"。( ) {{ select(16-2) }}
- 正确
- 错误
16-3. 假设数组 c 长度无限制,该程序所实现的算法的时间复杂度是 O(b) 的。( ) {{ select(16-3) }}
- 正确
- 错误
16-4. 函数 int logic(int x, int y) 的功能是( ) {{ select(16-4) }}
- 按位与
- 按位或
- 按位异或
- 以上都不是
16-5. 当输入为 "10 100 100" 时,输出的第 100 个数是( ) {{ select(16-5) }}
- 91
- 94
- 95
- 98
阅读程序(2)
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 const int P = 998244353, N = 1e4 + 10, M = 20;
6 int n, m;
7 string s;
8 int dp[1 << M];
9
10 int solve() {
11 dp[0] = 1;
12 for (int i = 0; i < n; i++) {
13 for (int j = (1 << (m - 1)) - 1; j >= 0; j--) {
14 int k = (j << 1) | (s[i] - '0');
15 if (j != 0 || s[i] == '1')
16 dp[k] = (dp[k] + dp[j]) % P;
17 }
18 }
19 int ans = 0;
20 for (int i = 0; i < (1 << m); i++) {
21 ans = (ans + 1ll * i * dp[i]) % P;
22 }
23 return ans;
24 }
25
26 int solve2() {
27 int ans = 0;
28 for (int i = 0; i < (1 << n); i++) {
29 int cnt = 0;
30 int num = 0;
31 for (int j = 0; j < n; j++) {
32 if (i & (1 << j)) {
33 num = num * 2 + (s[j] - '0');
34 cnt++;
35 }
36 }
37 if (cnt <= m) (ans += num) %= P;
38 }
39 return ans;
40 }
41
42 int main() {
43 cin >> n >> m;
44 cin >> s;
45 if (n <= 20) {
46 cout << solve2() << endl;
47 }
48 cout << solve() << endl;
49 return 0;
50 }
17-1. 函数 solve() 所实现的算法时间复杂度是 O(n * 2^m)。( ) {{ select(17-1) }}
- 正确
- 错误
17-2. 输入 "11 2 10000000001" 时,程序输出两个数 32 和 23。( ) {{ select(17-2) }}
- 正确
- 错误
17-3. 在 n <= 10 时,solve() 的返回值始终小于 4^10。( ) {{ select(17-3) }}
- 正确
- 错误
17-4. 当 n = 10 且 m = 10 时,有多少种输入使得两行的结果完全一致?( ) {{ select(17-4) }}
- 1024
- 11
- 10
- 0
17-5. 当 n <= 6 时,solve() 的最大可能返回值为?( ) {{ select(17-5) }}
- 65
- 211
- 665
- 2059
17-6. 若 n = 8,m = 8,solve 和 solve2 的返回值的最大可能的差值为?( ) {{ select(17-6) }}
- 1477
- 1995
- 2059
- 2187
阅读程序(3)
1 #include <iostream>
2 #include <cstring>
3 #include <algorithm>
4 using namespace std;
5
6 const int maxn = 1000000 + 5;
7 const int P1 = 998244353, P2 = 1000000007;
8 const int B1 = 2, B2 = 31;
9 const int K1 = 0, K2 = 13;
10
11 typedef long long ll;
12
13 int n;
14 bool p[maxn];
15 int p1[maxn], p2[maxn];
16
17 struct H {
18 int h1, h2, l;
19 H(bool b = false) {
20 h1 = b + K1;
21 h2 = b + K2;
22 l = 1;
23 }
24 H operator + (const H &h) const {
25 H hh;
26 hh.l = l + h.l;
27 hh.h1 = (1ll * h1 * p1[h.l] + h.h1) % P1;
28 hh.h2 = (1ll * h2 * p2[h.l] + h.h2) % P2;
29 return hh;
30 }
31 bool operator == (const H &h) const {
32 return l == h.l && h1 == h.h1 && h2 == h.h2;
33 }
34 bool operator < (const H &h) const {
35 if (l != h.l) return l < h.l;
36 else if (h1 != h.h1) return h1 < h.h1;
37 else return h2 < h.h2;
38 }
39 } h[maxn];
40
41 void init() {
42 memset(p, 1, sizeof(p));
43 p[0] = p[1] = false;
44 p1[0] = p2[0] = 1;
45 for (int i = 1; i <= n; i++) {
46 p1[i] = (1ll * B1 * p1[i - 1]) % P1;
47 p2[i] = (1ll * B2 * p2[i - 1]) % P2;
48 if (!p[i]) continue;
49 for (int j = 2 * i; j <= n; j += i) {
50 p[j] = false;
51 }
52 }
53 }
54
55 int solve() {
56 for (int i = n; i; i--) {
57 h[i] = H(p[i]);
58 if (2 * i + 1 <= n) {
59 h[i] = h[2 * i] + h[i] + h[2 * i + 1];
60 } else if (2 * i <= n) {
61 h[i] = h[2 * i] + h[i];
62 }
63 }
64 cout << h[1].h1 << endl;
65 sort(h + 1, h + n + 1);
66 int m = unique(h + 1, h + n + 1) - (h + 1);
67 return m;
68 }
69
70 int main() {
71 cin >> n;
72 init();
73 cout << solve() << endl;
74 }
18-1. 假设程序运行前能自动将 maxn 改为 n+1,所实现的算法的时间复杂度是 O(n log n)。( ) {{ select(18-1) }}
- 正确
- 错误
18-2. 时间开销的瓶颈是 init() 函数。( ) {{ select(18-2) }}
- 正确
- 错误
18-3. 若修改常数 B1 或 K1 的值,该程序可能会输出不同的结果。( ) {{ select(18-3) }}
- 正确
- 错误
18-4. 在 solve() 函数中,h[] 的合并顺序可以看作是:( ) {{ select(18-4) }}
- 二叉树的 BFS 序
- 二叉树的先序遍历
- 二叉树的中序遍历
- 二叉树的后序遍历
18-5. 输入 "10",输出的第一行是?( ) {{ select(18-5) }}
- 83
- 424
- 54
- 110101000
18-6. (4分)输入 "16",输出的第二行是?( ) {{ select(18-6) }}
- 7
- 9
- 10
- 12
三、完善程序
完善程序(1)(序列合并)
有两个长度为 N 的单调不降序列 A 和 B,序列的每个元素都是小于 10^9 的非负整数。在 A 和 B 中各取一个数相加可以得到 N^2 个和,求其中第 k 小的和。上述参数满足 N <= 10^5 和 1 <= K <= N^2。
1 #include <iostream>
2 using namespace std;
3 const int maxn = 100005;
4 int n;
5 long long k;
6 int a[maxn], b[maxn];
7
8 int *upper_bound(int *a, int *an, int ai) {
9 int l = 0, r = ___(1)___ ;
10 while (l < r) {
11 int mid = (l + r) >> 1;
12 if (___(2)___) {
13 r = mid;
14 } else {
15 l = mid + 1;
16 }
17 }
18 return ___(3)___ ;
19 }
20
21 long long get_rank(int sum) {
22 long long rank = 0;
23 for (int i = 0; i < n; i++) {
24 rank += upper_bound(b, b + n, sum - a[i]) - b;
25 }
26 return rank;
27 }
28
29 int solve() {
30 int l = 0, r = ___(4)___ ;
31 while (l < r) {
32 int mid = ((long long)l + r) >> 1;
33 if (___(5)___) {
34 l = mid + 1;
35 } else {
36 r = mid;
37 }
38 }
39 return l;
40 }
41
42 int main() {
43 cin >> n >> k;
44 for (int i = 0; i < n; ++i)
45 cin >> a[i];
46 for (int i = 0; i < n; ++i)
47 cin >> b[i];
48 cout << solve() << endl;
49 return 0;
50 }
19-1. ①处应填( ) {{ select(19-1) }}
- an - a
- an - a - 1
- ai
- ai + 1
19-2. ②处应填( ) {{ select(19-2) }}
- a[mid] > ai
- a[mid] >= ai
- a[mid] < ai
- a[mid] <= ai
19-3. ③处应填( ) {{ select(19-3) }}
- a + l
- a + l + 1
- a + l - 1
- an - l
19-4. ④处应填( ) {{ select(19-4) }}
- a[n-1] + b[n-1]
- a[n] + b[n]
- 2 * maxn
- maxn
19-5. ⑤处应填( ) {{ select(19-5) }}
- get_rank(mid) < k
- get_rank(mid) <= k
- get_rank(mid) > k
- get_rank(mid) >= k
完善程序(2)(次短路)
已知一个 n 个点 m 条边的有向图 G,并且给定图中的两个点 s 和 t,求次短路(长度严格大于最短路的最短路径)。如果不存在,输出一行 "-1"。如果存在,输出两行,第一行表示次短路的长度,第二行表示次短路的一个方案。
1 #include <cstdio>
2 #include <queue>
3 #include <utility>
4 #include <cstring>
5 using namespace std;
6 const int maxn = 2e5 + 10, maxm = 1e6 + 10, inf = 522133279;
7 int n, m, s, t;
8 int head[maxn], nxt[maxm], to[maxm], w[maxm], tot = 1;
9 int dis[maxn << 1], *dis2;
10 int pre[maxn << 1], *pre2;
11 bool vis[maxn << 1];
12
13 void add(int a, int b, int c) {
14 ++tot;
15 nxt[tot] = head[a];
16 to[tot] = b;
17 w[tot] = c;
18 head[a] = tot;
19 }
20
21 bool upd(int a, int b, int d, priority_queue<pair<int, int> > &q) {
22 if (d >= dis[b]) return false;
23 if (b < n) ___(1)___;
24 q.push(___(2)___);
25 dis[b] = d;
26 pre[b] = a;
27 return true;
28 }
29
30 void solve() {
31 priority_queue<pair<int, int> > q;
32 q.push(make_pair(0, s));
33 memset(dis, ___(3)___, sizeof(dis));
34 memset(pre, -1, sizeof(pre));
35 dis2 = dis + n;
36 pre2 = pre + n;
37 dis[s] = 0;
38 while (!q.empty()) {
39 int aa = q.top().second; q.pop();
40 if (vis[aa]) continue;
41 vis[aa] = true;
42 int a = aa % n;
43 for (int e = head[a]; e; e = nxt[e]) {
44 int b = to[e], c = w[e];
45 if (aa < n) {
46 if (!upd(a, b, dis[a] + c, q))
47 ___(4)___
48 } else {
49 upd(n + a, n + b, dis2[a] + c, q);
50 }
51 }
52 }
53 }
54
55 void out(int a) {
56 if (a != s) {
57 if (a < n) out(pre[a]);
58 else out(___(5)___);
59 }
60 printf("%d%c", a % n + 1, " \n"[a == n + t]);
61 }
62
63 int main() {
64 scanf("%d%d%d%d", &n, &m, &s, &t);
65 s--, t--;
66 for (int i = 0; i < m; ++i) {
67 int a, b, c;
68 scanf("%d%d%d", &a, &b, &c);
69 add(a - 1, b - 1, c);
70 }
71 solve();
72 if (dis2[t] == inf) puts("-1");
73 else {
74 printf("%d\n", dis2[t]);
75 out(n + t);
76 }
77 return 0;
78 }
20-1. ①处应填( ) {{ select(20-1) }}
- upd(pre[b], n + b, dis[b], q)
- upd(a, n + b, d, q)
- upd(pre[b], b, dis[b], q)
- upd(a, b, d, q)
20-2. ②处应填( ) {{ select(20-2) }}
- make_pair(-d, b)
- make_pair(d, b)
- make_pair(b, d)
- make_pair(-b, d)
20-3. ③处应填( ) {{ select(20-3) }}
- 0xff
- 0x1f
- 0x3f
- 0x7f
20-4. ④处应填( ) {{ select(20-4) }}
- upd(a, n + b, dis[a] + c, q)
- upd(n + a, n + b, dis2[a] + c, q)
- upd(n + a, b, dis2[a] + c, q)
- upd(a, b, dis[a] + c, q)
20-5. ⑤处应填( ) {{ select(20-5) }}
- pre2[a % n]
- pre[a % n]
- pre2[a]
- pre[a % n] + 1