跳到正文

个人记录

csp 初赛总结

yuer111约 20 分钟
原文
文章信息与版本

文章信息

发布
2026年7月29日
收录
2026年7月29日
更新
2026年11月11日
篇幅
约 20 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1

一. 基础与常识

  1. 对于如下代码段,其时间复杂度为()。
C++
int s = 0;
for (int i = 1; i <= n; i *= 2)
    for (int j = 1; j <= i; j++)
        s++;

A. O(n)\color{green} O(n) B. O(nlog⁡n)\color{red} O(n \log n) C. O(n2)O(n^2) D. O(log⁡n)O(\log n)

【考点】时间复杂度
【错因】没有举例手动分析。

【方法】设 nn 任意一个数,模拟就可以得到复杂度是 O(n)O(n)。外层循环变量 ii 的取值为 1, 2, 4, 8, … , 2k2^k(且 2k≤n2^k \le n)。内层循环运行 ii 次,因此总的执行次数为 1+2+4+...+2k1 + 2 + 4 + ... + 2^k。这是一个等比数列求和,结果为 2k+1−1≈2n2^{k+1} - 1 \approx 2n。忽略常数后,时间复杂度为 O(n)O(n)。


  1. 连接 nn 个字符串,每个字符串的长度不大于 mm,选择不同的实现方法会导致时间复杂度不一样。最坏情况下,该操作的时间复杂度是( )。
    A. O(nm)\color{red} O(nm) B. O(n2m)\color{green} O(n^2m) C. O(nm2)O(nm^2) D. O(log⁡n)O(\log n)

【考点】时间复杂度
【错因】不知道怎么分析

【正解】每次连接可能复制整个结果字符串,串长度可达到 O(nm)O(nm),最坏情况下的总时间复杂度为 O(n2m)O(n^2m)。


二. 排列组合

  1. 给定数字0、1、5、6、7、9,每个数字最多用一次,可能组成( )个正偶数。
    A. 216216 B. 480\color{red} 480 C. 586\color{green} 586 D. 589589

【考点】排列组合
【错因】算一位数的时候把 00 算进去了,所以后面也算错了。

【正解】一位偶数:只有数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=5861+9+36+108+216+216=586

三. 代码理解

第 kk 小差值
代码
C++
#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;
}

假设输入合法,且所有计算均不会溢出。

  1. 当数组 aa 已经按非降序排列时,ck(a,x,k)ck(a, x, k) 判断的是差值不超过 xx 的数对数量是否至少为 kk。()
    T\color{green} T F\color{red} F

【考点】程序阅读
【错因】没有读懂

【方法】ckck 函数使用双指针扫描。因为数组已排序,此时满足差值 ≤x\le x 的对数正好是 i−ji - j,累加后判断是否 ≥k\ge k。


四. 程序填空

折扣最短路
code

折扣最短路 给定一张 nn 个点、mm 条边的无向带权图,边权均为非负整数。给定起点 ss 和终点 tt。 现在有一张折扣卡,可以在经过某一条边时使用一次,使这条边的费用变为 ⌊w/2⌋\lfloor w/2 \rfloor。折扣卡最多使用一次,也可以不使用。 求从 ss 到 tt 的最小费用。

试补全程序。

C++
#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;
}
  1. ③处应填()。
    A. x.d==dis[x.u][x.c]‘x.d == dis[x.u][x.c]`
    B. x.d!=dis[x.u][x.c]\color{green} x.d != dis[x.u][x.c]
    C. x.u==tx.u == t
    D. x.c==1x.c == 1

【考点】dijkstra
我蒙的。

【正解】Dijkstra 的懒惰删除判定。当取出的距离 x.dx.d 大于或不等于当前记录的最小距离 dis[x.u][x.c]dis[x.u][x.c],代表该状态已被更优路径覆盖,直接 continue。


第 kk 小差值
code

第 kk 小差值

给定一个长度为 nn 的整数数组 aa,求所有满足 i<ji < j 的 ∣ai−aj∣|a_i - a_j| 中第 kk 小的值。

试补全程序。

C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100005;

int n;
ll k;
int a[N];

bool ck(int x) {
    ll s = 0;
    for (int i = 0, j = 0; i < n; i++) {
        while (①) j++;
        ②;
    }
    return ③;
}

int sol() {
    sort(a, a + n);
    int l = 0, r = ④;
    while (l < r) {
        int m = (l + r) >> 1;
        if (ck(m)) r = m;
        else ⑤;
    }
    return l;
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    cout << sol() << endl;
    return 0;
}
  1. ①处应填()。
    A. a[i]−a[j]>x\color{green} a[i] - a[j] > x
    B. a[i]−a[j]<xa[i] - a[j] < x
    C. a[j]−a[i]>x\color{red} a[j] - a[i] > x
    D. a[i]==a[j]a[i] == a[j]

【考点】程序阅读
【错因】没有搞清楚 a[i]a[i] 和 a[j]a[j] 的大小关系

【正解】solsol 函数里面已经排过序,且外循环里面有 i++,所以 a[i]>a[j]a[i]>a[j] 的。


  1. ⑤处应填()。
    A. r=m−1r = m - 1
    B. l=m\color{red} l = m
    C. l=m+1\color{green} l = m + 1
    D. r=m+1r = m + 1

【考点】程序阅读
【错因】选错了。。。



阅读程序
code
C++
#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;
}
  1. 如果希望从低位到高位依次输出n在p进制下的各位,ans += n / cur;这句应该改为( )。
    A. cout << n / cur << endl;
    B\color{red} B. cout << n % cur << endl;
    C. cout << n / cur % p << endl;
    D\color{green} D. cout << n * p / cur % p << endl;

【考点】代码理解
【错因】没读懂

【正解】先通过除法去掉低位部分,再mod  p\mod p 拿到最低位。因为 curcur 一开始初始化为 pp,而那个位应该是 nmod  pn\mod p,所以等价于先乘一个 pp 再除以 curcur。



树上第二类欧拉序
code

将树视为一个图,每条边视为 22 条(对向的)有向边,通过 DFS 遍历形成一个环游,遵循以下规则:从根节点开始遍历,每次进入一个节点 uu 时,立即将其记录到序列中:每次从 uu 的某个子节点返回到 uu 时,再次记录 uu。当节点 uu 有多个子节点时,按照子节点编号从小到大的顺序依次访问。最终得到的欧拉序列总长度为 2n−12n-1(nn为树的节点数),其中每个节点出现的次数等于其在树中的度数(根节点除外,由于初始进入会多记录一次,因此出现次数为度数加一)。

C++
#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;
}
  1. ②处应填( )。
    A\color{green} A. euler[timer] = u;
    B. euler[timer++] = u;
    C. euler[++timer] = u;
    D\color{red} D. euler[timer] = fa;

【考点】树上第二类欧拉序
【错因】没学过

【正解】进入 uu 为根节点的子树时,记录节点 uu 到序列中,timertimer 是一个时间戳,表示目前(第一次)访问到第几个节点,uu 追加在欧拉序列尾部。因为上一行 timertimer 已经加过了,所以这里不需要再自增。


  1. ③处应填( )。
    A. euler[timer] = u;
    B. euler[timer] = fa;
    C\color{red} C. euler[++timer] = u;
    D\color{green} D. euler[++timer] = fa;

【考点】树上第二类欧拉序

【正解】回溯完成节点 uu 的子树遍历后,回到父节点时要记录父节点,这里 timertimer 没有提前自增,所以要在选项里自增。


  1. ④处应填( )。
    A. sort(to, to + n);
    B. sort(to[u], to[u] + n);
    C\color{red} C. sort(to[u].begin(), to[u].begin() + to[u].size() - 1);
    D\color{green} D. sort(to[u].begin(),to[u].end());

【考点】树上第二类欧拉序

【正解】对每个节点 uu 的出边(to[u]to[u] 序列)按节点编号排序。sortsort 函数排序的参数区间为左闭右开 [begin,end)[begin, end),是 C++ 标准库的通用约定。对 vector 排序时,to[u].begin()to[u].begin() 为第一个元素的迭代器,to[u].end()to[u].end() 为最后一个元素之后的位置的迭代器。选项 A 和 B 存在语法错误,无法编译通过;选项 C 中结束位置不对。



中国邮路问题
code

邮递员需要从邮局出发,遍历他所负责的所有街道(每条街道至少经过一次),最后返回邮局。每条街道有不同的长度,请设计一条最短的路线。本质是在一个具有非负权的赋权连通图 GG 中,找出一条总权最小的环游。这种环游称为最优环游。若 GG 是欧拉图,则 GG 的任意欧拉环游都是最优环游。若 GG 不是欧拉图,则 GG 的任意一个环游必定经过某些边不止一次,需要通过添加重复边来构造欧拉图,且保证添加的边总权最小。

C++
#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;
}
  1. ③处应填( )。
    A\color{green} A. odd.empty()
    B. !odd.empty()
    C\color{red} C. odd.size() % 2 == 0
    D. odd.size() % 2 == 1

【考点】判断欧拉回路
【错因】看太快了没看到 oddodd 数组是存奇数的。。。

【正解】当不存在奇度顶点时,存在欧拉回路,则最优解就是原始的边权和。在无向图中奇度顶点必然是偶数个,所以选项 C 永远成立,选项 D 不可能成立。

原文