版本比较
csp 初赛总结
正文
增加 409 行删除 6 行
13 行未改动14151617181819202122222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104341051061071081091101111123611311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421524 行未改动240241242243752442452462472484 行未改动2532542552562572582592602612622632642652669426726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749813 行未改动
【考点】时间复杂度 【错因】没有举例手动分析。
【方法】设 $n$ 任意一个数,模拟就可以得到复杂度是 $O(n)$。或者看外循环,复杂度是 $O(\log n)$,然后内层循环 $j$ 也是与 $i$ 有关的,所以总体复杂度是 $O(n)$。【方法】设 $n$ 任意一个数,模拟就可以得到复杂度是 $O(n)$。外层循环变量 $i$ 的取值为 1, 2, 4, 8, ... , $2^k$(且 $2^k \le n$)。内层循环运行 $i$ 次,因此总的执行次数为 $1 + 2 + 4 + ... + 2^k$。这是一个等比数列求和,结果为 $2^{k+1} - 1 \approx 2n$。忽略常数后,时间复杂度为 $O(n)$。
---
## 二. 代码理解> 3. 连接 $n$ 个字符串,每个字符串的长度不大于 $m$,选择不同的实现方法会导致时间复杂度不一样。最坏情况下,该操作的时间复杂度是( )。 A. $\color{red} O(nm)$B. $\color{green} O(n^2m)$C. $O(nm^2)$D. $O(\log n)$
【考点】时间复杂度 【错因】不知道怎么分析
【正解】每次连接可能复制整个结果字符串,串长度可达到 $O(nm)$,最坏情况下的总时间复杂度为 $O(n^2m)$。
---
## 二. 排列组合
> 7. 给定数字0、1、5、6、7、9,每个数字最多用一次,可能组成( )个正偶数。 A. $216$B. $\color{red} 480$C. $\color{green} 586$D. $589$
【考点】排列组合 【错因】算一位数的时候把 $0$ 算进去了,所以后面也算错了。
【正解】一位偶数:只有数6,计1个。 两位偶数:个位为0有5个,个位为6有4个,计9个。 三位偶数:个位为0有20个,个位为6有16个,计36个。 四位偶数:个位为0有60个,个位为6有48个,计108个。 五位偶数:个位为0有120个,个位为6有96个,计216个。 六位偶数:个位为0有120个,个位为6有96个,计216个。 结果为 $1+9+36+108+216+216=586$
## 三. 代码理解::::info[第 $k$ 小差值]:::info[代码]```cpp#include <bits/stdc++.h>using namespace std;using ll = long long;
bool ck(vector<int>& a, int x, ll k) { ll s = 0; for (int i = 0, j = 0; i < (int)a.size(); i++) { while (a[i] - a[j] > x) j++; s += i - j; } return s >= k;}
int sol(vector<int> a, ll k) { sort(a.begin(), a.end()); int l = 0, r = a.back() - a[0]; while (l < r) { int m = (l + r) / 2; if (ck(a, m, k)) r = m; else l = m + 1; } return l;}
int main() { int n; ll k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; cout << sol(a, k) << endl; return 0;}```假设输入合法,且所有计算均不会溢出。:::> 23. 当数组 $a$ 已经按非降序排列时,$ck(a, x, k)$ 判断的是差值不超过 $x$ 的数对数量是否至少为 $k$。() > $\color{green} T$ $\color{red} F$
【考点】程序阅读 【错因】没有读懂
【方法】$ck$ 函数使用双指针扫描。因为数组已排序,此时满足差值 $\le x$ 的对数正好是 $i - j$,累加后判断是否 $\ge k$。::::
---
## 三. 程序填空## 四. 程序填空::::info[折扣最短路]:::info[code]折扣最短路给定一张 $n$ 个点、$m$ 条边的无向带权图,边权均为非负整数。给定起点 $s$ 和终点 $t$。现在有一张折扣卡,可以在经过某一条边时使用一次,使这条边的费用变为 $\lfloor w/2 \rfloor$。折扣卡最多使用一次,也可以不使用。求从 $s$ 到 $t$ 的最小费用。
::::info[code]试补全程序。```cpp#include <bits/stdc++.h>using namespace std;using ll = long long;const int N = 100005;const ll INF = (1LL << 60);
struct Ed { int v, w;};
struct Sta { ll d; int u, c; bool operator<(const Sta& o) const { return ①; }};
int n, m, s, t;vector<Ed> g[N];ll dis[N][2];
int main() { cin >> n >> m >> s >> t; for (int i = 1; i <= m; i++) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); } for (int i = 1; i <= n; i++) dis[i][0] = dis[i][1] = INF; priority_queue<Sta> q; ②; q.push({0, s, 0}); while (!q.empty()) { Sta x = q.top(); q.pop(); if (③) continue; for (auto e : g[x.u]) { int v = e.v; int c = x.c; if (④ < dis[v][c]) { dis[v][c] = ④; q.push({dis[v][c], v, c}); } if (!c && ⑤ < dis[v][1]) { dis[v][1] = ⑤; q.push({dis[v][1], v, 1}); } } } cout << min(dis[t][0], dis[t][1]) << endl; return 0;}```:::
> 36. ③处应填()。 A. $x.d == dis[x.u][x.c]`$ B. $\color{green} x.d != dis[x.u][x.c]$ C. $x.u == t$ D. $x.c == 1$
【考点】dijkstra 我蒙的。
【正解】Dijkstra 的懒惰删除判定。当取出的距离 $x.d$ 大于或不等于当前记录的最小距离 $dis[x.u][x.c]$,代表该状态已被更优路径覆盖,直接 continue。
::::
---
::::info[第 $k$ 小差值]:::info[code]### 第 $k$ 小差值
给定一个长度为 $n$ 的整数数组 $a$,求所有满足 $i < j$ 的 $|a_i - a_j|$ 中第 $k$ 小的值。
试补全程序。
```cpp#include <bits/stdc++.h>using namespace std;using ll = long long;const int N = 100005;
int n;ll k;int a[N];
24 行未改动 cout << sol() << endl; return 0;}```:::::::
> 39. ①处应填()。 A. $\color{green} a[i] - a[j] > x$ B. $a[i] - a[j] < x$ 4 行未改动【错因】没有搞清楚 $a[i]$ 和 $a[j]$ 的大小关系
【正解】$sol$ 函数里面已经排过序,且外循环里面有 `i++`,所以 $a[i]>a[j]$ 的。
---
> 43. ⑤处应填()。 A. $r = m - 1$ B. $\color{red} l = m$ C. $\color{green} l = m + 1$ D. $r = m + 1$
【考点】程序阅读【考点】程序阅读 【错因】选错了。。。
::::
---
---
::::info[阅读程序]:::info[code]```cpp#include <iostream>using namespace std;
int main() { int n, p; cin >> n >> p; //保证n为正整数,p为素数,cur不会溢出 long long 类型 int ans = 0; long long cur = p; while (cur <= n) { ans += n / cur; cur *= p; } cout << ans << endl; return 0;}```:::
> 19. 如果希望从低位到高位依次输出n在p进制下的各位,`ans += n / cur;`这句应该改为( )。 A. `cout << n / cur << endl;` $\color{red} B$. `cout << n % cur << endl;` C. `cout << n / cur % p << endl;` $\color{green} D$. `cout << n * p / cur % p << endl;`
【考点】代码理解 【错因】没读懂
【正解】先通过除法去掉低位部分,再$\mod p$ 拿到最低位。因为 $cur$ 一开始初始化为 $p$,而那个位应该是 $n\mod p$,所以等价于先乘一个 $p$ 再除以 $cur$。
::::
---
---
::::info[树上第二类欧拉序]:::info[code]将树视为一个图,每条边视为 $2$ 条(对向的)有向边,通过 DFS 遍历形成一个环游,遵循以下规则:从根节点开始遍历,每次进入一个节点 $u$ 时,立即将其记录到序列中:每次从 $u$ 的某个子节点返回到 $u$ 时,再次记录 $u$。当节点 $u$ 有多个子节点时,按照子节点编号从小到大的顺序依次访问。最终得到的欧拉序列总长度为 $2n-1$($n$为树的节点数),其中每个节点出现的次数等于其在树中的度数(根节点除外,由于初始进入会多记录一次,因此出现次数为度数加一)。```cpp#include <bits/stdc++.h>using namespace std;const int N = 1009;vector<int> to[N];int n, timer, euler[ ① ];
void dfs(int u, int fa) { ② ++timer; for (int i = 0; i < to[u].size(); ++i) { if (to[u][i] != fa) dfs(to[u][i], u); } ③ }
int main() { cin >> n; //保证输入的 1<=u, v<=n<=1000,且无环 for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; to[u].push_back(v); to[v].push_back(u); } for (int u = 1; u <= n; ++u) { ④ } dfs(1, 0); for (int i = 1; ⑤ ; ++i) cout << euler[i] << " "; cout << euler[2 * n - 1] << endl; return 0;}```:::
> 32. ②处应填( )。 $\color{green} A$. `euler[timer] = u;` B. `euler[timer++] = u;` C. `euler[++timer] = u;` $\color{red} D$. `euler[timer] = fa;`
【考点】树上第二类欧拉序 【错因】没学过
【正解】进入 $u$ 为根节点的子树时,记录节点 $u$ 到序列中,$timer$ 是一个时间戳,表示目前(第一次)访问到第几个节点,$u$ 追加在欧拉序列尾部。因为上一行 $timer$ 已经加过了,所以这里不需要再自增。
---
> 33. ③处应填( )。 A. `euler[timer] = u;` B. `euler[timer] = fa;` $\color{red} C$. `euler[++timer] = u;` $\color{green} D$. `euler[++timer] = fa;`
【考点】树上第二类欧拉序
【正解】回溯完成节点 $u$ 的子树遍历后,回到父节点时要记录父节点,这里 $timer$ 没有提前自增,所以要在选项里自增。
---
> 34. ④处应填( )。 A. `sort(to, to + n);` B. `sort(to[u], to[u] + n);` $\color{red} C$. `sort(to[u].begin(), to[u].begin() + to[u].size() - 1);` $\color{green} D$. `sort(to[u].begin(),to[u].end());`
【考点】树上第二类欧拉序
【正解】对每个节点 $u$ 的出边($to[u]$ 序列)按节点编号排序。$sort$ 函数排序的参数区间为左闭右开 $[begin, end)$,是 C++ 标准库的通用约定。对 vector 排序时,$to[u].begin()$ 为第一个元素的迭代器,$to[u].end()$ 为最后一个元素之后的位置的迭代器。选项 A 和 B 存在语法错误,无法编译通过;选项 C 中结束位置不对。
::::
---
---
::::info[中国邮路问题]:::info[code]邮递员需要从邮局出发,遍历他所负责的所有街道(每条街道至少经过一次),最后返回邮局。每条街道有不同的长度,请设计一条最短的路线。本质是在一个具有非负权的赋权连通图 $G$ 中,找出一条总权最小的环游。这种环游称为最优环游。若 $G$ 是欧拉图,则 $G$ 的任意欧拉环游都是最优环游。若 $G$ 不是欧拉图,则 $G$ 的任意一个环游必定经过某些边不止一次,需要通过添加重复边来构造欧拉图,且保证添加的边总权最小。
```cpp#include <iostream>#include <vector>#include <algorithm>using namespace std;typedef long long ll;
int main() { int n, m; cin >> n >> m; ll total = 0; vector<vector<int> > dst(n + 1, vector<int>(n + 1, INT_MAX)); vector<int> deg(n + 1, 0); for (int i = 1; i <= n; ++i) dst[i][i] = 0; for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; if ( /* ① */ ) { dst[u][v] = w; dst[v][u] = w; } deg[u]++; deg[v]++; total += w; } // Floyd-Warshall 算法:计算所有顶点对的最短路径 for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { if (dst[i][k] == INT_MAX) continue; for (int j = 1; j <= n; ++j) { if (dst[k][j] == INT_MAX) continue; if ( /* ② */ ) { dst[i][j] = dst[i][k] + dst[k][j]; } } } } vector<int> odd; for (int i = 1; i <= n; ++i) if (deg[i] % 2 == 1) odd.push_back(i); if ( /* ③ */ ) { cout << total << endl; return 0; } int k = odd.size(); vector<long long> dp(1 << k, LLONG_MAX); // dp数组用于状态压缩DP,大小为2^k,初始化为极大值 dp[0] = 0; // 枚举所有匹配状态(用二进制位表示顶点是否已匹配) for (int mask = 0; mask < (1 << k); ++mask) { if (dp[mask] == LLONG_MAX) continue; //找到第一个未匹配的顶点(二进制位中表示未匹配) int i = 0; while ( /* ④ */ ) i++; if (i >= k) continue; // 尝试将顶点i与另一个未匹配的顶点j配对 for (int j = i + 1; j < k; ++j) { if (mask & (1 << j)) continue; if (dst[odd[i]][odd[j]] == INT_MAX) continue; // 新状态:将i和j标记为已匹配 int new_mask = ( /* ⑤ */ ); ll new_val = dp[mask] + dst[odd[i]][odd[j]]; if (new_val < dp[new_mask]) dp[new_mask] = new_val; } } ll ans = total + dp[(1 << k) - 1]; cout << ans << endl; return 0;}```:::
> 38. ③处应填( )。 $\color{green} A$. `odd.empty()` B. `!odd.empty()` $\color{red} C$. `odd.size() % 2 == 0` D. `odd.size() % 2 == 1`
【考点】判断欧拉回路 【错因】看太快了没看到 $odd$ 数组是存奇数的。。。
【正解】当不存在奇度顶点时,存在欧拉回路,则最优解就是原始的边权和。在无向图中奇度顶点必然是偶数个,所以选项 C 永远成立,选项 D 不可能成立。
::::