#CSPS2026CSMN11. 信友队2026mn1
信友队2026mn1
2026CSP-S第一轮(初赛)模拟赛(第1场)试题
一、单项选择题(共 15 题,每题 2 分,共计 30 分;每题有且仅有一个正确选项)
- 在常见的 32 位操作系统中,C++ 数据类型 int 和指针变量(Pointer)分别通常占用( )个字节。 {{ select(1) }}
- 2 和 4
- 4 和 4
- 4 和 8
- 8 和 8
- 十进制数 20.25_(10) 与八进制数 20.26_(8) 的和,用十六进制表示是( ) {{ select(2) }}
- 24.51_(16)
- 24.98_(16)
- 28.51_(16)
- 28.98_(16)
- 下列 IP 地址中,格式完全合法的 IPv4 主机地址是( )。 {{ select(3) }}
- 192.168.1.256
- 10.0.0.1
- 256.256.256.256
- 172.16.0.0.1
- 在C++中,执行int x=6120; cout << (x&-x); 后,输出的结果是( )。 {{ select(4) }}
- 1
- 4
- 8
- 6112
- 分析以下 C++ 循环结构,其时间复杂度为( )。 {{ select(5) }}
- O(log n)
- O(n)
- O(n log n)
- O(n^2)
int sum = 0;
for (int i = 1; i <= n; i *= 2) {
for (int j = 1; j <= i; j++) {
sum++;
}
}
- 在一个大小为 11 的哈希表中,使用闭散列法的线性探测解决冲突,哈希函数为 H(key)=key mod 11(下标从 0 开始,探测到表尾后回到表头继续)。依次插入关键字 40, 2, 14, 51, 7, 25, 62。插入 62 后,它最终被放置在哪个索引位置( ) {{ select(6) }}
- 7
- 8
- 9
- 10
- 现有如下 C++ 程序,其运行后的输出结果是( ) {{ select(7) }}
- 3 9 9
- 3 12 9
- 6 9 6
- 6 12 6
#include <iostream>
using namespace std;
int main() {
int a[5] = {3, 6, 9, 12, 15};
int *p = a + 1;
cout << p[0] << " ";
cout << *(p + 2) << " ";
cout << *(a + 3) - *p << endl;
return 0;
}
- 在有向图的最短路算法中,如果图中所有边的权值均是非负数,求单源最短路径最常用且基于贪心思想的高效算法是( )。 {{ select(8) }}
- Prim 算法
- Kruskal 算法
- Dijkstra 算法
- Floyd-Warshall 算法
- 5 个互不相同的顶点(编号1~5),一共可以构成( )种不同的无根树(两棵树只要边的连接方式不同就算不同,不指定根结点)。 {{ select(9) }}
- 3
- 120
- 125
- 625
- 从1~9这九个数字中取出3个不同数字组成三位数,要求三个数字互不相邻(例如不能选 2、3、5,因为2和3是相邻的数字;即选的3个数字中不能有相差为1的数字),一共有( )个这样的三位数。 {{ select(10) }}
- 105
- 120
- 210
- 240
- 在 1~126000 中与 126000 互质的整数共有( )个。 {{ select(11) }}
- 19200
- 28800
- 33600
- 57600
- 以下关于图和树的说法,正确的是( )。 {{ select(12) }}
- 有 n 个点 n-1 条边的简单无向图是树
- 连通无向图恰有 2 个奇度顶点,则存在欧拉回路
- n 个点的无重边的有向无环图(DAG)最多有 (n(n-1))/(2) 条边
- 简单有向图中入度最大的顶点出度一定最小
- 在 C++ 中,假设有两个非负整数 x 和 y,表达式 (x ^ y) ^ x 的最终结果等价于( )。(注:^ 表示按位异或) {{ select(13) }}
- x
- y
- 0
- x + y
- 现有 3 种物品,每种物品数量无限。A物品重量 3 价值 4 ,B物品重量 4 价值 7 ,C物品重量 7 价值 12 。在背包容量为 749 的前提下,装入物品的总价值最大为( )。 {{ select(14) }}
- 1308
- 1309
- 1310
- 1311
- 下图为一个包含 12 个顶点的有向图,该图的强连通分量数目是( )
{{ select(15) }}
- 2
- 5
- 6
- 9
二、阅读程序
阅读程序(1)
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 using namespace std;
5
6 bool check(const vector<int>& a, int d, int c) {
7 int cnt = 1;
8 int last = a[0];
9 for (int i = 1; i < a.size(); i++) {
10 if (a[i] - last >= d) {
11 cnt++;
12 last = a[i];
13 }
14 }
15 return cnt >= c;
16 }
17
18 int main() {
19 int n, c;
20 cin >> n >> c;
21 vector<int> a(n);
22 for (int i = 0; i < n; i++) cin >> a[i];
23 sort(a.begin(), a.end());
24
25 int left = 1, right = a[n - 1] - a[0];
26 int ans = 0;
27
28 while (left <= right) {
29 int mid = left + (right - left) / 2;
30 if (check(a, mid, c)) {
31 ans = mid;
32 left = mid + 1;
33 } else {
34 right = mid - 1;
35 }
36 }
37 cout << ans << endl;
38 return 0;
39 }
16-1. 该程序解决的问题可以抽象为:在一条直线上选择 c 个点,使得相邻两个被选点之间的最大距离尽可能小。( ) {{ select(16-1) }}
- 正确
- 错误
16-2. check 函数中运用了贪心思想:只要当前位置与上一个被选位置的距离达到 d,就毫不犹豫地选择当前位置。( ) {{ select(16-2) }}
- 正确
- 错误
16-3. 若将 left = 1 改为 left = 0,对于任何测试数据,程序的输出结果一定会发生改变。( ) {{ select(16-3) }}
- 正确
- 错误
16-4. 二分查找的区间 [left, right] 表示尚未排除、且可能改进当前 ans 的候选范围。( ) {{ select(16-4) }}
- 正确
- 错误
16-5. 如果输入为
5 3
1 2 8 4 9
则输出为( )。 {{ select(16-5) }}
- 2
- 3
- 4
- 5
16-6. 假设数组 a 的长度为 N,最大元素与最小元素的差值为 D。则整个程序的整体时间复杂度为( )。 {{ select(16-6) }}
- O(N^2)
- O(N log N)
- O(N log D)
- O(N log N + N log D)
阅读程序(2)
1 #include <cstdio>
2 #include <vector>
3 #include <map>
4 using namespace std;
5 typedef long long LL;
6 typedef map<int,int> mpII;
7 typedef pair<int,LL> prIL;
8 int n,a[100002];
9 vector<int> G[100002];
10 void mrg(mpII &A,prIL &p,mpII &B,prIL &q)
11 {
12 if(A.size()<B.size()) swap(A,B),swap(p,q);
13 for(auto item : B) if(item.second)
14 {
15 A[item.first]+=item.second;
16 if(p.first<A[item.first]) p=prIL(A[item.first],item.first);
17 else if(p.first==A[item.first]) p.second+=item.first;
18 }
19 }
20 mpII dfs(int u,int fa,prIL &maxx,LL &ans)
21 {
22 mpII mpu;
23 mpu[a[u]]++;
24 maxx=prIL(1,a[u]);
25 for(int v : G[u]) if(v!=fa)
26 {
27 prIL pr;
28 mpII mpv=dfs(v,u,pr,ans);
29 mrg(mpu,maxx,mpv,pr);
30 }
31 ans+=maxx.second;
32 return mpu;
33 }
34 int main()
35 {
36 scanf("%d",&n);
37 for(int i=1;i<=n;i++) scanf("%d",a+i);
38 for(int i=1;i<n;i++)
39 {
40 int u,v;
41 scanf("%d%d",&u,&v);
42 G[u].push_back(v);
43 G[v].push_back(u);
44 }
45 prIL maxx;
46 LL ans=0;
47 dfs(1,0,maxx,ans);
48 printf("%lld",ans);
49 return 0;
50 }
所有输入都是100000以内的正整数。
17-1. 输入3 2 1 2 3 2 1 2,程序输出5。( ) {{ select(17-1) }}
- 正确
- 错误
17-2. 程序第28行的赋值语句在两个map间进行了复制操作,这个操作的时间复杂度是O(n),会拖慢整个程序的时间,需要优化掉。( ) {{ select(17-2) }}
- 正确
- 错误
17-3. 若输入的a数组内的所有数字都相同(设为 x),则程序输出为 n × x。( ) {{ select(17-3) }}
- 正确
- 错误
17-4. 该算法的时间复杂度为( ) {{ select(17-4) }}
- O(n)
- O(n^2)
- O(n log n)
- O(n log^2 n)
17-5. 若输入为:
7
1 2 1 3 2 1 2
1 2
1 3
2 4
2 5
3 6
6 7
则程序输出为( ) {{ select(17-5) }}
- 9
- 12
- 14
- 16
17-6. 若将 mrg 函数中的 if(A.size()<B.size()) 改为 if(A.size()>B.size()),则下列说法正确的是( ) {{ select(17-6) }}
- 程序输出不变,但时间复杂度会变大
- 程序输出不变,时间复杂度也不变
- 程序输出可能会改变
- 程序会运行崩溃
阅读程序(3)
1 #include <algorithm>
2 #include <cstdio>
3 #include <cstring>
4 #define ll long long
5
6 const int N = 100005;
7
8 int n;
9 int a[N];
10 int cnt_check = 0;
11 int seed = 1;
12
13 inline int check(int x, int y) {
14 ++cnt_check;
15 if (x < y) return -1;
16 if (x > y) return 1;
17 return 0;
18 }
19
20 inline int rnd(int mod) {
21 seed = seed * 48271LL % 2147483647;
22 return seed % mod;
23 }
24
25 inline int kth(int l, int r, int k) {
26 while (l <= r) {
27 int x = a[l + rnd(r - l + 1)];
28 int i = l, j = l, p = r;
29
30 while (j <= p) {
31 int t = check(a[j], x);
32 if (t < 0) {
33 std::swap(a[i], a[j]);
34 ++i;
35 ++j;
36 } else if (t > 0) {
37 std::swap(a[j], a[p]);
38 --p;
39 } else {
40 ++j;
41 }
42 }
43
44 if (k < i) {
45 r = i - 1;
46 } else if (k > p) {
47 l = p + 1;
48 } else {
49 return x;
50 }
51 }
52 return -1;
53 }
54
55 inline void solve1() {
56 int ans = kth(1, n, (n + 1) / 2);
57
58 int cnt = 0;
59 for (int i = 1; i <= n; ++i) {
60 if (check(ans, a[i]) == 0) {
61 ++cnt;
62 }
63 }
64
65 if (cnt * 2 > n) {
66 printf("%d %d\n", ans, cnt_check);
67 } else {
68 printf("-1 %d\n", cnt_check);
69 }
70 }
71
72 inline void solve2() {
73 int ans = 0, cnt = 0;
74
75 for (int i = 1; i <= n; ++i) {
76 if (cnt == 0) {
77 ans = a[i];
78 cnt = 1;
79 } else if (check(ans, a[i]) == 0) {
80 ++cnt;
81 } else {
82 --cnt;
83 }
84 }
85
86 cnt = 0;
87 for (int i = 1; i <= n; ++i) {
88 if (check(ans, a[i]) == 0) {
89 ++cnt;
90 }
91 }
92
93 if (cnt * 2 > n) {
94 printf("%d %d\n", ans, cnt_check);
95 } else {
96 printf("-1 %d\n", cnt_check);
97 }
98 }
99
100 int main() {
101 scanf("%d", &n);
102
103 for (int i = 1; i <= n; ++i) {
104 scanf("%d", &a[i]);
105 }
106
107 int t;
108 scanf("%d", &t);
109
110 if (t == 1) {
111 solve1();
112 } else {
113 solve2();
114 }
115
116 return 0;
117 }
(注意:下述的“检查次数”为调用 check 函数的次数,即 cnt_check 的值。所有输入满足 1≤ n≤ 10^5,1≤ a_i≤ 10^9,t∈{1,2}。)
18-1. 当输入为
7
1 2 1 3 1 1 4
2
时,程序输出 1 11。( ) {{ select(18-1) }}
- 正确
- 错误
18-2. 若将 solve1 中得到 ans 后的所有代码(即第58~69行)替换为
printf("%d %d\n", ans, cnt_check);
则对于任意满足条件的输入,程序输出的第一个整数都不会改变。( ) {{ select(18-2) }}
- 正确
- 错误
18-3. 若某个数在序列中出现了 m 次,且 2m>n,则执行 solve2 时,第一个 for 循环结束后的 cnt 一定等于 2m-n。( ) {{ select(18-3) }}
- 正确
- 错误
18-4. 若
n = 10
a = {8, 4, 4, 2, 7, 4, 1, 6, 4, 5}
则执行 solve1 时,函数 kth(1,n,(n+1)/2) 的返回值为( )。 {{ select(18-4) }}
- 2
- 4
- 5
- 与 rnd 函数产生的随机数有关
18-5. 假设 rnd(mod) 每次独立、等概率地返回 0 到 mod-1 中的一个整数,那么关于 solve1 和 solve2 的时间复杂度,下列说法正确的是( )。 {{ select(18-5) }}
- solve1 的期望时间复杂度和最坏时间复杂度均为 O(n),solve2 的最坏时间复杂度为 O(n)
- solve1 的期望时间复杂度为 O(n),最坏时间复杂度为 O(n^2),solve2 的最坏时间复杂度为 O(n)
- solve1 的期望时间复杂度为 O(nlog n),最坏时间复杂度为 O(n^2),solve2 的最坏时间复杂度为 O(n)
- solve1 的期望时间复杂度为 O(n),最坏时间复杂度为 O(n^2),solve2 的最坏时间复杂度为 O(nlog n)
18-6. 当 n=100 时,执行 solve2,检查次数最少和最多分别可能为( )。 {{ select(18-6) }}
- 100,199
- 149,199
- 150,199
- 150,200
三、完善程序
完善程序(1)(特殊最短路)
给定一个 N × M 的网格迷宫,. 表示空地,# 表示障碍物(墙)。你可以向上下左右四个方向移动。走到空地的代价为 0;走到墙壁的代价为 1(相当于打破这堵墙)。求从左上角 (0,0) 走到右下角 (N-1, M-1) 所需要打破的最小墙壁数量。
试补全程序。
1 #include <iostream>
2 #include <vector>
3 #include <deque>
4 #include <string>
5 using namespace std;
6
7 const int INF = 1e9;
8 int dx[4] = {-1, 1, 0, 0};
9 int dy[4] = {0, 0, -1, 1};
10
11 int main() {
12 int n, m;
13 cin >> n >> m;
14 vector<string> grid(n);
15 for (int i = 0; i < n; ++i) cin >> grid[i];
16
17 vector<vector<int>> dist(n, vector<int>(m, INF));
18 deque<pair<int, int>> dq;
19
20 dist[0][0] = 0;
21 ①;
22
23 while (!dq.empty()) {
24 auto curr = dq.front();
25 dq.pop_front();
26 int x = curr.first, y = curr.second;
27
28 for (int i = 0; i < 4; ++i) {
29 int nx = x + dx[i];
30 int ny = y + dy[i];
31
32 if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
33 int cost = ②;
34
35 if (dist[x][y] + cost < dist[nx][ny]) {
36 dist[nx][ny] = ③;
37 if (cost == 0) {
38 ④;
39 } else {
40 ⑤;
41 }
42 }
43 }
44 }
45 }
46 cout << dist[n - 1][m - 1] << endl;
47 return 0;
48 }
19-1. ①处应填( ) {{ select(19-1) }}
- dq.push_back({0, 0})
- dq.push_back({n-1, m-1})
- dq.push_front({1, 1})
- dq.empty()
19-2. ②处应填( ) {{ select(19-2) }}
- grid[nx][ny] == '.' ? 1 : 0
- grid[nx][ny] == '#' ? 1 : 0
- 1
- 0
19-3. ③处应填( ) {{ select(19-3) }}
- dist[nx][ny] + cost
- dist[x][y] + 1
- dist[x][y] + cost
- cost
19-4. ④处应填( ) {{ select(19-4) }}
- dq.push_back({nx, ny})
- dq.push_front({nx, ny})
- dq.push_front({x, y})
- dq.pop_front()
19-5. ⑤处应填( ) {{ select(19-5) }}
- dq.push_front({nx, ny})
- dq.push_back({nx, ny})
- dq.push_back({x, y})
- dq.pop_back()
完善程序(2)(最大子段异或)
星际舰队截获了一段由外星文明发送的长度为 N 的高频信号序列 A。根据情报,信号的“异常度”被定义为序列中某一段连续子序列(可以为空)中所有数字的按位异或和(XOR Sum)。 现在需要你编写程序,找出这段信号序列中异常度最大的那一段子序列,并输出其最大的异或和。
试补全程序。
1 #include <iostream>
2 #include <vector>
3 #include <algorithm>
4 using namespace std;
5
6 const int MAXN = 100005;
7 int trie[MAXN * 32][2], tot = 0;
8
9 void insert(int val) {
10 int p = 0;
11 for (int i = 30; i >= 0; i--) {
12 int bit = ①;
13 if (!trie[p][bit]) {
14 trie[p][bit] = ++tot;
15 }
16 p = trie[p][bit];
17 }
18 }
19
20 int query(int val) {
21 int p = 0, res = 0;
22 for (int i = 30; i >= 0; i--) {
23 int bit = (val >> i) & 1;
24 if (②) {
25 p = trie[p][bit ^ 1];
26 res |= ③;
27 } else {
28 p = ④;
29 }
30 }
31 return res;
32 }
33
34 int main() {
35 int n;
36 cin >> n;
37 int pre_xor = 0, max_anomaly = 0;
38
39 insert(0);
40
41 for (int i = 1; i <= n; i++) {
42 int x;
43 cin >> x;
44 pre_xor ^= x;
45 max_anomaly = max(max_anomaly, ⑤);
46 insert(pre_xor);
47 }
48
49 cout << max_anomaly << endl;
50 return 0;
51 }
20-1. ①处应填( ) {{ select(20-1) }}
- val & (1 << i)
- (val >> i) & 1
- val >> 1
- val ^ (1 << i)
20-2. ②处应填( ) {{ select(20-2) }}
- trie[p][bit]
- trie[p][bit ^ 1] != 0
- trie[p][bit ^ 1] == 0
- trie[p][bit] != 0
20-3. ③处应填( ) {{ select(20-3) }}
- (1 << i)
- bit
- (bit ^ 1) << i
- 1
20-4. ④处应填( ) {{ select(20-4) }}
- trie[p][bit ^ 1]
- trie[p][0]
- trie[p][1]
- trie[p][bit]
20-5. ⑤处应填( ) {{ select(20-5) }}
- query(x)
- query(pre_xor)
- pre_xor
- query(pre_xor ^ x)