#C1218. CSP 2024 提高级第一轮
CSP 2024 提高级第一轮
一、单项选择题
(共 15 题,每题 2 分,共计 30 分;每题有且仅有一个正确选项)
【第 1 题】
在 Linux 系统中,如果你想显示当前工作目录的路径,应该使用哪个命令?( )。 {{ select(1) }}
- pwd
- cd
- ls
- echo
【第 2 题】 假设一个长度为 的整数数组中每个元素值互不相同,且这个数组是无序的。 要找到这个数组中最大元素的时间复杂度是多少?( )
{{ select(2) }}
【第 3 题】
在 C++ 中,以下哪个函数调用会造成栈溢出?( )
{{ select(3) }}
int foo() { return 0; }
int bar() { int x = 1; return x; }
void baz() { int a[1000]; baz(); }
void qux() { return; }
【第 4 题】
在一场比赛中,有 名选手参加,前三名将获得金、银、铜牌。若不允许并列,且每名选手只能获得一枚奖牌,则不同的颁奖方式共有多少种?( )
{{ select(4) }}
【第 5 题】
下面哪个数据结构最适合实现先进先出(FIFO)的功能?( ) {{ select(5) }}
- 栈
- 队列
- 线性表
- 二叉搜索树
【第 6 题】
已知 ,且对于 有 ,则 的值为?
{{ select(6) }}
【第 7 题】
假设有一个包含 n 个顶点的无向图,且该图是欧拉图。以下关于该图的描述中哪一项不一定正确? ( ) {{ select(7) }}
- 所有顶点的度数均为偶数
- 该图连通
- 该图存在一个欧拉回路
- 该图的边数是奇数
【第 8 题】
对数组进行二分查找的过程中,以下哪个条件必须满足?( ) {{ select(8) }}
- 数组必须是有序的
- 数组必须是无序的
- 数组长度必须是 2 的幂
- 数组中的元素必须是整数
【第 9 题】 考虑一个自然数 以及一个模数 ,你需要计算 的逆元(即 在模 意义下的逆元)。以下哪种算法最为适合?( )
{{ select(9) }}
- 使用暴力法逐次尝试
- 使用扩展欧几里得算法
- 使用快速幂算法
- 使用线性筛法
【第 10 题】 在设计一个哈希表时,为了减少冲突,需要使用适当的哈希函数和冲突解决策略。如果哈希表中有 个键值对,表的装载因子为 ()。在使用开放地址法来解决冲突的过程中,最坏情况下查找一个元素的时间复杂度为?( ) {{ select(10) }}
【第 11 题】
假设有一棵 层的完全二叉树,该树最多包含多少个结点?( ) {{ select(11) }}
【第 12 题】
设有一个 个顶点的完全图,每两个顶点之间都有一条边。至少有多少个长度为 的环?( )
{{ select(12) }}
【第 13 题】 对于一个整数 ,定义 为 的各位数字之和,问使得 的最小自然数 是多少?( )
{{ select(13) }}
【第 14 题】 设有一个长度为 的 字符串,其中有 个 。每次操作可以交换相邻的两个字符。在最坏情况下将这 个 移到字符串最右边所需要的交换次数是多少?( ) {{ select(14) }}
【第 15 题】
如图是一张包含 个顶点的有向图,如果要删除其中一些边,使得从节点 到节点 没有可行路径,并且删除的边数最少,请问总共有多少种可行的删除边的集合?( )。
{{ select(15) }}
- 1
- 2
- 3
- 4
二、阅读程序
(程序输入不超过数组或字符串定义的范围;判断题正确填 √,错误填 ⨉ ;除特殊说明外,判断题 1.5 分,选择题 3 分,共计 40 分)
阅读程序(一)
01 #include <iostream>
02 using namespace std;
03
04 const int N = 1000;
05 int c[N];
06
07 int logic(int x, int y) {
08 return (x & y) ^ ((x ^ y) | (~x & y));
09 }
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 void recursion(int depth, int *arr, int size) {
17 if (depth <= 0 || size <= 1) return;
18 int pivot = arr[0];
19 int i = 0, j = size - 1;
20 while (i <= j) {
21 while (arr[i] < pivot) i++;
22 while (arr[j] > pivot) j--;
23 if (i <= j) {
24 int temp = arr[i];
25 arr[i] = arr[j];
26 arr[j] = temp;
27 i++; j--;
28 }
29 }
30 recursion(depth - 1, arr, j + 1);
31 recursion(depth - 1, arr + i, size - i);
32 }
33
34 int main() {
35 int a, b, d;
36 cin >> a >> b >> d;
37 generate(a, b, c);
38 recursion(d, c, b);
39 for (int i = 0; i < b; ++i) cout << c[i] << " ";
40 cout << endl;
41 }
假设输入的所有数都为不超过 1000 的正整数,完成下面的判断题和单选题:
判断题
- 当 时,输出的序列是有序的。 {{ select(16) }}
- 正确
- 错误
- 当输入 时,输出为 。 {{ select(17) }}
- 正确
- 错误
- 假设数组 长度无限制,该程序所实现的算法的时间复杂度是 的。 {{ select(18) }}
- 正确
- 错误
单选题
- 函数
int logic(int x, int y)
的功能是( )。 {{ select(19) }}
- 按位与
- 按位或
- 按位异或
- 以上都不是
- 当输入为 10 100 100 时,输出的第 100 个数是?( )。 {{ select(20) }}
- 91
- 94
- 95
- 98
阅读程序(二)
01 #include <iostream>
02 #include <string>
03 using namespace std;
04
05 const int P = 998244353, N = 1e4 + 10, M = 20;
06 int n, m;
07 string s;
08 int dp[1 << M];
09
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 }
假设输入的 是包含 个字符的 01 串,完成下面的判断题和单选题。
判断题
-
假设数组
dp
长度无限制,函数solve()
所实现的算法的时间复杂度是 。{{ select(21) }}
- 正确
- 错误
- 输入
11 2 10000000001
时,程序输出两个数 32 和 23。 {{ select(22) }}
- 正确
- 错误
- (2 分)在 时,
solve()
的返回值始终小于 。
{{ select(23) }}
- 正确
- 错误
单选题
- 当 且 时,有多少种输入使得两行的结果完全一致?( ) {{ select(24) }}
- 1024
- 11
- 10
- 0
- 当 时,
solve()
的最大可能返回值为( )? {{ select(25) }}
- 65
- 211
- 665
- 2059
- 若 ,
solve
和solve2
的返回值的最大可能差值为( )? {{ select(26) }}
- 1477
- 1995
- 2059
- 2187
阅读程序(三)
01 #include <iostream>
02 #include <cstring>
03 #include <algorithm>
04 using namespace std;
05
06 const int maxn = 1000000 + 5;
07 const int P1 = 998244353, P2 = 1000000007;
08 const int B1 = 2, B2 = 31;
09 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
25 H operator + (const H & h) const {
26 H hh;
27 hh.l = l + h.l;
28 hh.h1 = (1ll * h1 * p1[h.l] + h.h1) % P1;
29 hh.h2 = (1ll * h2 * p2[h.l] + h.h2) % P2;
30 return hh;
31 }
32
33 bool operator == (const H & h) const {
34 return l == h.l && h1 == h.h1 && h2 == h.h2;
35 }
36
37 bool operator < (const H & h) const {
38 if (l != h.l) return l < h.l;
39 else if (h1 != h.h1) return h1 < h.h1;
40 else return h2 < h.h2;
41 }
42 } h[maxn];
43
44 void init() {
45 memset(p, 1, sizeof(p));
46 p[0] = p[1] = false;
47 p1[0] = p2[0] = 1;
48 for (int i = 1; i <= n; ++i) {
49 p1[i] = (1ll * B1 * p1[i-1]) % P1;
50 p2[i] = (1ll * B2 * p2[i-1]) % P2;
51 if (!p[i]) continue;
52 for (int j = 2 * i; j <= n; j += i) {
53 p[j] = false;
54 }
55 }
56 }
57
58 int solve() {
59 for (int i = n; i; --i) {
60 h[i] = H(p[i]);
61 if (2 * i + 1 <= n) {
62 h[i] = h[2 * i] + h[i] + h[2 * i + 1];
63 } else if (2 * i <= n) {
64 h[i] = h[2 * i] + h[i];
65 }
66 }
67
68 cout << h[1].h1 << endl;
69 sort(h + 1, h + n + 1);
70 int m = unique(h + 1, h + n + 1) - (h + 1);
71 return m;
72 }
73
74 int main() {
75 cin >> n;
76 init();
77 cout << solve() << endl;
78 }
判断题
-
假设程序运行前能自动将
maxn
改为 ,所实现的算法的时间复杂度是 。{{ select(27) }}
- 正确
- 错误
- 时间开销的瓶颈是
init()
函数。 {{ select(28) }}
- 正确
- 错误
- 若修改常数 或 的值,该程序可能会输出不同的结果。
{{ select(29) }}
- 正确
- 错误
单选题
- 在
solve()
函数中,h[l]
的合并顺序可以看作( )? {{ select(30) }}
- 二叉树的 BFS 序
- 二叉树的先序遍历
- 二叉树的中序遍历
- 二叉树的后序遍历
- 输入 ,输出的第一行是?( ) {{ select(31) }}
- 83
- 424
- 54
- 110101000
-
输入 ,输出的第二行是?( )
{{ select(32) }}
- 7
- 9
- 10
- 12
三、程序填空
(单选题,每小题 3 分,共计 30 分)
程序填空(一)
- (序列合并) 有两个长度为 的单调不降序列 和 ,序列的每个元素都是小于 的非负整数。在 和 中各取一个数相加可以得到 个和,求其中第 小的和。上述参数满足 和 。
#include <iostream>
using namespace std;
const int maxn = 100005;
int n;
long long k;
int a[maxn], b[maxn];
int* upper_bound(int *a, int *an, int ai) {
int l = 0, r = ___①___;
while (l < r) {
int mid = (l+r)>>1;
if (___②___) {
r = mid;
} else {
l = mid + 1;
}
}
return ___③___;
}
long long get_rank(int sum) {
long long rank = 0;
for (int i = 0; i < n; ++i) {
rank += upper_bound(b, b+n, sum - a[i]) - b;
}
return rank;
}
int solve() {
int l = 0, r = ___④___;
while (l < r) {
int mid = ((long long)l+r)>>1;
if (___⑤___) {
l = mid + 1;
} else {
r = mid;
}
}
return l;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) cin >> b[i];
cout << solve() << endl;
}
- ① 处应填( )。 {{ select(33) }}
- an-a
- an-a-1
- ai
- ai+1
- ② 处应填( )。 {{ select(34) }}
- a[mid] > ai
- a[mid] >= ai
- a[mid] < ai
- a[mid] <= ai
- ③ 处应填( )。 {{ select(35) }}
- a+l
- a+l+1
- a+l-1
- an-l
- ④ 处应填( )。 {{ select(36) }}
- a[n-1]+b[n-1]
- a[n]+b[n]
- 2 * maxn
- maxn
- ⑤ 处应填( )。 {{ select(37) }}
- get_rank(mid) < k
- get_rank(mid) <= k
- get_rank(mid) > k
- get_rank(mid) >= k
完善程序(二)
2.(次短路) 已知一个有 个点 条边的有向图 ,并且给定图中的两个点 和 ,求次短路(长度严格大于最短路的最短路径)。如果不存在,输出一行 。如果存在,输出两行,第一行表示次短路的长度,第二行表示次短路的一个方案。
#include <cstdio>
#include <queue>
#include <utility>
#include <cstring>
using namespace std;
const int maxn = 2e5+10, maxm = 1e6+10, inf = 522133279;
int n, m, s, t;
int head[maxn], nxt[maxm], to[maxm], w[maxm], tot = 1;
int dis[maxn<<1], *dis2;
int pre[maxn<<1], *pre2;
bool vis[maxn<<1];
void add(int a, int b, int c) {
++tot;
nxt[tot] = head[a];
to[tot] = b;
w[tot] = c;
head[a] = tot;
}
bool upd(int a, int b, int d, priority_queue<pair<int, int>> &q) {
if (d >= dis[b]) return false;
if (b < n) ___①___;
q.push(___②___);
dis[b] = d;
pre[b] = a;
return true;
}
void solve() {
priority_queue<pair<int, int>> q;
q.push(make_pair(0, s));
memset(dis, ___③___, sizeof(dis));
memset(pre, -1, sizeof(pre));
dis2 = dis+n;
pre2 = pre+n;
dis[s] = 0;
while (!q.empty()) {
int aa = q.top().second; q.pop();
if (vis[aa]) continue;
vis[aa] = true;
int a = aa % n;
for (int e = head[a]; e; e = nxt[e]) {
int b = to[e], c = w[e];
if (aa < n) {
if (!upd(a, b, dis[a]+c, q))
___④__;
} else {
upd(n+a, n+b, dis2[a]+c, q);
}
}
}
void out(int a) {
if (a != s) {
if (a < n) out(pre[a]);
else out(___⑤___);
}
printf("%d%c", a%n+1, " \n"[a == n+t]);
}
int main() {
scanf("%d%d%d", &n, &m, &s, &t);
s--, t--;
for (int i = 0; i < m; ++i) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a-1, b-1, c);
}
solve();
if (dis2[t] == inf) puts("-1");
else {
printf("%d\n", dis2[t]);
out(n+t);
}
}
- ① 处应填( )。 {{ select(38) }}
- 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)
- ② 处应填( )。 {{ select(39) }}
- make_pair(-d, b)
- make_pair(d, b)
- make_pair(b, d)
- make_pair(-b, d)
- ③ 处应填( )。 {{ select(40) }}
- 0xff
- 0x1f
- 0x3f
- 0x7f
- ④ 处应填( )。 {{ select(41) }}
- 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)
- ⑤ 处应填( )。 {{ select(42) }}
- pre2[a%n]
- pre[a%n]
- pre2[a]
- pre[a%n]+1