#CSPS2021CS. 2021CSPS初赛
2021CSPS初赛
2021 CSP-S 初赛试题
一、单项选择题(共15题,每题2分,共计30分;每题有且仅有一个正确选项)
- 在 Linux 系统终端中,用于列出当前目录下所含的文件和子目录的命令为( )。{{ select(1) }}
- ls
- cd
- cp
- all
- 二进制数 00101010₂ 和 00010110₂ 的和为( )。{{ select(2) }}
- 00111100₂
- 01000000₂
- 00111100₂
- 01000010₂
- 在程序运行过程中,如果递归调用的层数过多,可能会由于( )引发错误。{{ select(3) }}
- 系统分配的栈空间溢出
- 系统分配的队列空间溢出
- 系统分配的链表空间溢出
- 系统分配的堆空间溢出
- 以下排序方法中,( )是不稳定的。{{ select(4) }}
- 插入排序
- 冒泡排序
- 堆排序
- 归并排序
- 以比较为基本运算,对于 2n 个数,同时找到最大值和最小值,最坏情况下需要的最小的比较次数为( )。{{ select(5) }}
- 4n−2
- 3n+1
- 3n−2
- 2n+1
- 现有一个地址区间为 0∼10 的哈希表,对于出现冲突情况,会往后找第一个空的地址存储(到 10 冲突了就从 0 开始往后),现在要依次存储 (0,1,2,3,4,5,6,7),哈希函数为 h(x)=x² mod 11。请问 7 存储在哈希表哪个地址中( )。{{ select(6) }}
- 5
- 6
- 7
- 8
- G 是一个非连通简单无向图(没有自环和重边),共有 36 条边,则该图至少有( )个点。{{ select(7) }}
- 8
- 9
- 10
- 11
- 令根结点的高度为 1,则一棵含有 2021 个结点的二叉树的高度至少为( )。{{ select(8) }}
- 10
- 11
- 12
- 2021
- 前序遍历和中序遍历相同的二叉树为且仅为( )。{{ select(9) }}
- 只有 1 个点的二叉树
- 根结点没有左子树的二叉树
- 非叶子结点只有左子树的二叉树
- 非叶子结点只有右子树的二叉树
- 定义一种字符串操作为交换相邻两个字符。将 "DACFEB" 变为 "ABCDEF" 最少需要( )次上述操作。{{ select(10) }}
- 7
- 8
- 9
- 6
- 有如下递归代码
则 solve(23,23) 的结果为( )。{{ select(11) }}solve(t, n): if t=1 return 1 else return 5 * solve(t-1, n) mod n
- 1
- 7
- 12
- 22
- 斐波那契数列的定义为:F₁=1, F₂=1, Fₙ=Fₙ₋₁+Fₙ₋₂ (n≥3)。现在用如下程序来计算斐波那契数列的第 n 项,其时间复杂度为( )。
{{ select(12) }}F(n): if n<=2 return 1 else return F(n-1) + F(n-2)
- O(n)
- O(n²)
- O(2ⁿ)
- O(n log n)
- 有 8 个苹果从左到右排成一排,你要从中挑选至少一个苹果,并且不能同时挑选相邻的两个苹果,一共有( )种方案。 {{ select(13) }}
- 36
- 48
- 54
- 64
- 设一个三位数 n = abc(a,b,c 均为 1∼9 之间的整数),若以 a、b、c 作为三角形的三条边可以构成等腰三角形(包括等边),则这样的 n 有( )个。 {{ select(14) }}
- 81
- 120
- 165
- 216
- 有如下的有向图,节点为 A,B,...,J,其中每条边的长度都标在图中。则节点 A 到节点 J 的最短路径长度为( )。 {{ select(15) }}
- 16
- 19
- 20
- 22

二、阅读程序
(1)
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 const double r = acos(0.5);
6
7 int a1, b1, c1, d1;
8 int a2, b2, c2, d2;
9
10 inline int sq(const int x) { return x * x; }
11 inline int cu(const int x) { return x * x * x; }
12
13 int main()
14 {
15 cout.flags(ios::fixed);
16 cout.precision(4);
17
18 cin >> a1 >> b1 >> c1 >> d1;
19 cin >> a2 >> b2 >> c2 >> d2;
20
21 int t = sq(a1 - a2) + sq(b1 - b2) + sq(c1 - c2);
22
23 if(t <= sq(d2 - d1)) cout << cu(min(d1, d2)) * r * 4;
24 else if(t >= sq(d1 + d2)) cout << 0;
25 else{
26 double x = d1 - (sq(d1) - sq(d2) + t) / sqrt(t) / 2;
27 double y = d2 - (sq(d2) - sq(d1) + t) / sqrt(t) / 2;
28 cout << (x * x * (3 * d1 - x) + y * y * (3 * d2 - y)) * r;
29 }
30 cout << endl;
31 return 0;
32 }
假设输入的所有数的绝对值都不超过 1000,完成下面的判断题和单选题:
- 将第 21 行中 t 的类型声明从 int 改为 double,不会影响程序运行的结果。( ){{ select(16) }}
- 正确
- 错误
- 将第 26、27 行中的 / sqrt(t) / 2 替换为 / 2 / sqrt(t),不会影响程序运行的结果。( ){{ select(17) }}
- 正确
- 错误
- 将第 28 行中的 x * x 改成 sq(x)、y * y 改成 sq(y),不会影响程序运行的结果。( ){{ select(18) }}
- 正确
- 错误
- (2 分)当输入为 0 0 0 1 1 0 0 1 时,输出为1.3090。( ){{ select(19) }}
- 正确
- 错误
- 当输入为 1 1 1 1 1 1 1 2 时,输出为( )。{{ select(20) }}
- 3.1416
- 6.2832
- 4.7124
- 4.1888
- (2.5 分)这段代码的含义为( )。{{ select(21) }}
- 求圆的面积并
- 求球的体积并
- 求球的体积交
- 求椭球的体积并
(2)
1 #include <algorithm>
2 #include <iostream>
3 using namespace std;
4
5 int n, a[1005];
6
7 struct Node
8 {
9 int h, j, m, w;
10
11 Node(const int _h, const int _j, const int _m, const int _w) :
12 h(_h), j(_j), m(_m), w(_w)
13 {}
14
15 Node operator+(const Node &o) const
16 {
17 return Node(
18 max(h, w + o.h),
19 max(max(j, o.j), m + o.h),
20 max(m + o.w, o.m),
21 w + o.w);
22 }
23 };
24
25 Node solve1(int h, int m)
26 {
27 if(h > m)
28 return Node(-1, -1, -1, -1);
29 if(h == m)
30 return Node(max(a[h], 0), max(a[h], 0), max(a[h], 0), a[h]);
31 int j = (h + m) >> 1;
32 return solve1(h, j) + solve1(j + 1, m);
33 }
34
35 int solve2(int h, int m)
36 {
37 if(h > m)
38 return -1;
39 if(h == m)
40 return max(a[h], 0);
41 int j = (j + m) >> 1;
42 int wh = 0, wm = 0;
43 int wht = 0, wmt = 0;
44 for(int i = j; i >= h; i--){
45 wht += a[i];
46 wh = max(wh, wht);
47 }
48 for(int i = j + 1; i <= m; i++){
49 wmt += a[i];
50 wm = max(wm, wmt);
51 }
52 return max(max(solve2(h, j), solve2(j + 1, m)), wh + wm);
53 }
54
55 int main() {
56 cin >> n;
57 for(int i = 1; i <= n; i++) cin >> a[i];
58 cout << solve1(1, n).j << endl;
59 cout << solve2(1, n) << endl;
60 return 0;
61 }
- 程序总是会正常执行并输出两行两个相等的数。( ){{ select(22) }}
- 正确
- 错误
- 第 28 行与第 38 行分别有可能执行两次及以上。( ){{ select(23) }}
- 正确
- 错误
- 当输入为 5 -10 11 -9 5 -7 时,输出的第二行为 7。( ){{ select(24) }}
- 正确
- 错误
- solve1(1, n) 的时间复杂度为( )。{{ select(25) }}
- O(log n)
- O(n)
- O(n log n)
- O(n!)
- solve2(1, n) 的时间复杂度为( )。{{ select(26) }}
- O(log n)
- O(n)
- O(n log n)
- O(n!)
- 当输入为 10 -3 2 10 0 -8 9 -4 -5 9 4 时,输出的第一行为( )。{{ select(27) }}
- 13
- 17
- 24
- 12
(3)
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 char base[64];
6 char table[256];
7
8 void init()
9 {
10 for(int i = 0; i < 26; i++) base[i] = 'A' + i;
11 for(int i = 0; i < 26; i++) base[26 + i] = 'a' + i;
12 for(int i = 0; i < 10; i++) base[52 + i] = '0' + i;
13 base[62] = '+', base[63] = '/';
14
15 for(int i = 0; i < 256; i++) table[i] = 0xff;
16 for(int i = 0; i < 64; i++) table[base[i]] = i;
17 table['='] = 0;
18 }
19
20 string encode(string str)
21 {
22 string ret;
23 int i;
24 for(int i = 0; i + 3 <= str.size(); i += 3){
25 ret += base[str[i] >> 2];
26 ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
27 ret += base[(str[i + 1] & 0x0f) << 2 | str[i + 2] >> 6];
28 ret += base[str[i + 2] & 0x3f];
29 }
30 if(i < str.size()){
31 ret += base[str[i] >> 2];
32 if(i + 1 == str.size()){
33 ret += base[(str[i] & 0x03) << 4];
34 ret += "==";
35 }
36 else{
37 ret += base[(str[i] & 0x03) << 4 | str[i + 1] >> 4];
38 ret += base[(str[i + 1] & 0x0f) << 2];
39 ret += "=";
40 }
41 }
42 return ret;
43 }
44
45 string decode(string str)
46 {
47 string ret;
48 int i;
49 for(int i = 0; i < str.size(); i += 4){
50 ret += table[str[i]] << 2 | table[str[i + 1]] >> 4;
51 if(str[i + 2] != '=')
52 ret += (table[str[i + 1]] & 0x0f) << 4 | table[str[i + 2]] >> 2;
53 if(str[i + 3] != '=')
54 ret += table[str[i + 2]] << 6 | table[str[i + 3]];
55 }
56 return ret;
57 }
58
59 int main()
60 {
61 init();
62 cout << int(table[0]) << endl;
63 int opt;
64 string str;
65 cin >> opt >> str;
66 cout << (opt ? decode(str) : encode(str)) << endl;
67 return 0;
68 }
- 程序总是先输出一行一个整数,再输出一行一个字符串。( ){{ select(28) }}
- 正确
- 错误
- 对于任意不含空白字符的字符串 str1,先执行程序输入 0 str1,得到输出的第二行记为 str2;再执行程序输入 1 str2,输出的第二行必为 str1。( ){{ select(29) }}
- 正确
- 错误
- 当输入为 1 SGVsbG93b3JsZA== 时,输出的第二行为 HelloWorld。( ){{ select(30) }}
- 正确
- 错误
- 设输入字符串长度为 n,encode 函数的时间复杂度为( )。{{ select(31) }}
- Θ(√n)
- O(n)
- O(n log n)
- O(n!)
- 输出的第一行为( )。{{ select(32) }}
- 0xff
- 255
- 0xFF
- -1
- (4 分)当输入为 0 CSP2021csp 时,输出的第二行为( )。{{ select(33) }}
- Q1NQMjAyMWNzcAv=
- Q1NQMjAyMGNzcA==
- Q1NQMjAyMGNzcAv=
- Q1NQMjAyMWNzcA==
三、完善程序(单选题,每小题 3 分,共计 30 分)
(1) 魔法数字
小 H 的魔法数字是 4。给定 n,他希望用若干个 4 进行若干次加法、减法和整除运算得到 n。但由于小 H 计算能力有限,计算过程中只能出现不超过 M = 10000 的正整数。求至少可能用到多少个 4。
例如,当 n = 2 时,有 2 = (4 + 4)/4,用到了 3 个 4,是最优方案。
1 #include <iostream>
2 #include <cstdlib>
3 #include <climits>
4
5 using namespace std;
6
7 const int M = 10000;
8 bool Vis[M + 1];
9 int F[M + 1];
10
11 void update(int &x, int y) {
12 if (y < x)
13 x = y;
14 }
15
16 int main() {
17 int n;
18 cin >> n;
19 for (int i = 0; i <= M; i++)
20 F[i] = INT_MAX;
21 /* ① */; // 需要填充
22 int r = 0;
23 while (/* ② */) { // 需要填充
24 r++;
25 int x = 0;
26 for (int i = 1; i <= M; i++)
27 if (/* ③ */) // 需要填充
28 x = i;
29 Vis[x] = 1;
30 for (int i = 1; i <= M; i++)
31 if (/* ④ */) { // 需要填充
32 int t = F[i] + F[x];
33 if (i + x <= M)
34 update(F[i + x], t);
35 if (i != x)
36 update(F[abs(i - x)], t);
37 if (i % x == 0)
38 update(F[i / x], t);
39 if (x % i == 0)
40 update(F[x / i], t);
41 }
42 }
43 cout << F[n] << endl;
44 return 0;
45 }
- ①处应填( )。{{ select(34) }}
- F[4] = 0
- F[1] = 4
- F[1] = 2
- F[4] = 1
- ②处应填( )。{{ select(35) }}
- !Vis[n]
- r < n
- F[M] == INT_MAX
- F[n] == INT_MAX
- ③处应填( )。{{ select(36) }}
- F[i] == r
- !Vis[i] && F[i] == r
- F[i] < F[x]
- !Vis[i] && F[i] < F[x]
- ④处应填( )。{{ select(37) }}
- F[i] < F[x]
- F[i] <= r
- Vis[i]
- i <= x
(2) (RMQ 区间最值问题)
给定序列 a₁,...,aₙ,和 m 次询问,每次询问给定 l, r,求 max{a₁,...,aₙ}。
为了解决该问题,有一个算法叫 the Method of Four Russians,其时间复杂度为 O(n + m),步骤如下:
- 建立 Cartesian(笛卡尔)树,将问题转化为树上的 LCA(最近公共祖先)问题。
- 对于 LCA 问题,可以考虑其 Euler 序,即求 Euler 序列上两点间一个新的 RMQ 问题。
- 注意新的问题为 ±1 RMQ,即相邻两点的深度差一定为 1。
下面解决这个 ±1 RMQ 问题,“序列”指 Euler 序列。设 t 为 Euler 序列长度。取 b = ⌈log₂t / 2⌉。将序列每 b 个分为一大块,使用 ST 表处理大块间的 RMQ 问题,复杂度 O((t/b) log t) = O(m)。对于一个块内的 RMQ 问题,也需要 O(1) 的算法。由于差分数组有 2^(b-1) 种,可以预处理出所有情况下的最值位置,预处理复杂度 O(b·2^b),不超过 O(n)。最终,对于一个查询,可以转化为中间整的大块的 RMQ 问题,以及两端块内的 RMQ 问题。
试补全程序。
1 #include <iostream>
2 #include <cmath>
3
4 using namespace std;
5
6 const int MAXN = 100000, MAXT = MAXN << 1;
7 const int MAXL = 18, MAXB = 9, MAXC = MAXT / MAXB;
8
9 struct node {
10 int val;
11 int dep, dfn, end;
12 node *son[2]; // son[0], son[1] 分别表示左右儿子
13 } T[MAXN];
14
15 int n, t, b, c, Log2[MAXC + 1];
16 int Pos[(1 << (MAXB - 1)) + 5], Dif[MAXC + 1];
17 node *root, *A[MAXT], *Min[MAXL][MAXC];
18
19 void build() { // 建立 Cartesian 树
20 static node *S[MAXN + 1];
21 int top = 0;
22 for (int i = 0; i < n; i++) {
23 node *p = &T[i];
24 while (top && S[top]->val < p->val)
25 /* ① */; // 需要填充
26 if (top)
27 /* ② */; // 需要填充
28 S[++top] = p;
29 }
30 root = S[1];
31 }
32
33 void DFS(node *p) { // 构建 Euler 序列
34 A[p->dfn = t++] = p;
35 for (int i = 0; i < 2; i++)
36 if (p->son[i]) {
37 p->son[i]->dep = p->dep + 1;
38 DFS(p->son[i]);
39 A[t++] = p;
40 }
41 p->end = t - 1;
42 }
43
44 node *min(node *x, node *y) {
45 return /* ③ */ ? x : y; // 需要填充比较条件
46 }
47
48 void ST_init() {
49 b = (int)(ceil(log2(t) / 2));
50 c = t / b;
51 Log2[1] = 0;
52 for (int i = 2; i <= c; i++)
53 Log2[i] = Log2[i >> 1] + 1;
54 for (int i = 0; i < c; i++) {
55 Min[0][i] = A[i * b];
56 for (int j = 1; j < b; j++)
57 Min[0][i] = min(Min[0][i], A[i * b + j]);
58 }
59 for (int i = 1, l = 2; l <= c; i++, l <<= 1)
60 for (int j = 0; j + l <= c; j++)
61 Min[i][j] = min(Min[i - 1][j], Min[i - 1][j + (l >> 1)]);
62 }
63
64 void small_init() { // 块内预处理
65 for (int i = 0; i <= c; i++)
66 for (int j = 1; j < b && i * b + j < t; j++)
67 if (/* ④ */) // 需要填充条件
68 Dif[i] |= 1 << (j - 1);
69 for (int S = 0; S < (1 << (b - 1)); S++) {
70 int mx = 0, v = 0;
71 for (int i = 1; i < b; i++) {
72 /* ⑤ */ // 需要填充更新 v 的语句
73 if (v < mx) {
74 mx = v;
75 Pos[S] = i;
76 }
77 }
78 }
79 }
80
81 node *ST_query(int l, int r) {
82 int g = Log2[r - l + 1];
83 return min(Min[g][l], Min[g][r - (1 << g) + 1]);
84 }
85
86 node *small_query(int l, int r) { // 块内查询
87 int p = l / b;
88 int S = /* ⑥ */; // 需要填充计算 S 的表达式
89 return A[l + Pos[S]];
90 }
91
92 node *query(int l, int r) {
93 if (l > r)
94 return query(r, l);
95 int pl = l / b, pr = r / b;
96 if (pl == pr) {
97 return small_query(l, r);
98 } else {
99 node *s = min(small_query(l, pl * b + b - 1), small_query(pr * b, r));
100 if (pl + 1 <= pr - 1)
101 s = min(s, ST_query(pl + 1, pr - 1));
102 return s;
103 }
104 }
105
106 int main() {
107 int m;
108 cin >> n >> m;
109 for (int i = 0; i < n; i++)
110 cin >> T[i].val;
111 build();
112 DFS(root);
113 ST_init();
114 small_init();
115 while (m--) {
116 int l, r;
117 cin >> l >> r;
118 cout << query(T[l].dfn, T[r].dfn)->val << endl;
119 }
120 return 0;
121 }
- ①处应填( )。{{ select(38) }}
- p->son[0] = S[top--]
- p->son[1] = S[top--]
- S[top--]->son[0] = p
- S[top--]->son[1] = p
- ②处应填( )。{{ select(39) }}
- p->son[0] = S[top]
- p->son[1] = S[top]
- S[top]->son[0] = p
- S[top]->son[1] = p
- ③处应填( )。{{ select(40) }}
- x->dep < y->dep
- x < y
- x->dep > y->dep
- x->val < y->val
- ④处应填( )。{{ select(41) }}
- A[i * b + j - 1] == A[i * b + j]->son[0]
- A[i * b + j]->val < A[i * b + j - 1]->val
- A[i * b + j] == A[i * b + j - 1]->son[1]
- A[i * b + j]->dep < A[i * b + j - 1]->dep
- ⑤处应填( )。{{ select(42) }}
- v += (S >> i & 1) ? -1 : 1
- v += (S >> i & 1) ? 1 : -1
- v += (S >> (i – 1) & 1) ? 1 : -1
- v += (S >> (i – 1) & 1) ? -1 : 1
- ⑥处应填( )。{{ select(43) }}
- (Dif[p] >> (r – p * b)) & ((1 << (r – l)) – 1)
- Dif[p]
- (Dif[p] >> (l – p * b)) & ((1 << (r – l)) – 1)
- (Dif[p] >> ((p + 1) * b – r)) & ((1 << (r – l + 1)) – 1)