题解 | CVZOI Round 2 A-F题 题解

· · 个人记录

这里是 2025ZP-早六五班-出题组 举办的 CVZOI Round 2 的A-G题题解

废话不多说,直接看题解

A题 通用签到题:

点击跳转

代码如下:

#include<iostream>
using namespace std;
int main(){
    cout << "I love RDFZ!";
    return 0;
}

很简单,不多说了。

B题 白昼流星:

点击跳转

很简单的贪心题目,只要认真听讲,每次选择结束时间最早的即可。

因为所有流星幸运值相同,总幸运值 = 选中数量 × V。

  1. 按 l_i 排序
  2. 遍历,若 t_i \ge 上一个结束时间,则选
  3. 答案 = 选中数 × V

时间复杂度 O(n \log n),能过 n=2000。

代码如下:

#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 2005;
struct Meteor {
    int start;  // t_i
    int end;    // l_i
} meteors[MAX_N];
// 排序规则:按结束时间升序排列(贪心核心)
bool compare(Meteor a, Meteor b) {
    return a.end < b.end;
}
int main() {
    int n, T, L, V;
    cin >> n >> T >> L >> V;
    for (int i = 0; i < n; ++i) {
        cin >> meteors[i].start >> meteors[i].end;
    }
    sort(meteors, meteors + n, compare);
    int count = 0;
    int last_end = -1;
    for (int i = 0; i < n; ++i) {
        // 流星时间在[T, L]范围内,且与上一个选中的流星不重叠
        if (meteors[i].start >= last_end && meteors[i].end <= L) {
            count++;
            last_end = meteors[i].end;
        }
    }
    cout << count * V << endl;
    return 0;
}

C题 密码检查:

点击跳转

题解思路

核心:依次检查四个条件,任一不满足即输出 WRONG。

检查顺序:

  1. 长度检查:密码长度是否在 [L, R] 范围内。
  2. 字符类型检查:
    • 遍历字符串,统计是否出现大写字母、小写字母、数字。
    • 同时检查是否有其他非法字符(非字母数字)。
  3. 连续字符检查:
    • 遍历每连续三个字符:
      • 判断是否完全相同(aaa 型)。
      • 判断 ASCII 码是否严格连续递增 1(abc、123 型)。
      • 判断 ASCII 码是否严格连续递减 1(cba、ZYX 型)。
    • 注意:只有同类型字符(同为数字/大写/小写)才可能构成连续递增/递减。

特殊说明:

代码如下:

#include <cstdio>
#include <cstring>

char s[105];

int main() {
    int L, R;
    scanf("%d %d", &L, &R);
    scanf("%s", s);

    int len = strlen(s);

    // 条件1: 长度在L到R之间(根据样例,应该是闭区间)
    if (len < L || len > R) {
        printf("WRONG\n");
        return 0;
    }

    // 条件2和3一起检查
    int has_upper = 0, has_lower = 0, has_digit = 0;
    int has_other = 0;

    for (int i = 0; i < len; ++i) {
        char c = s[i];
        if (c >= 'A' && c <= 'Z') {
            has_upper = 1;
        } else if (c >= 'a' && c <= 'z') {
            has_lower = 1;
        } else if (c >= '0' && c <= '9') {
            has_digit = 1;
        } else {
            has_other = 1;  // 出现其他字符
            break;  // 直接退出,不合法
        }
    }

    if (has_other || !has_upper || !has_lower || !has_digit) {
        printf("WRONG\n");
        return 0;
    }

    // 条件4: 检查连续三个字符
    for (int i = 0; i + 2 < len; ++i) {
        // 连续三个相同
        if (s[i] == s[i+1] && s[i] == s[i+2]) {
            printf("WRONG\n");
            return 0;
        }

        // 连续严格递增1
        if (s[i]+1 == s[i+1] && s[i+1]+1 == s[i+2]) {
            printf("WRONG\n");
            return 0;
        }

        // 连续严格递减1
        if (s[i]-1 == s[i+1] && s[i+1]-1 == s[i+2]) {
            printf("WRONG\n");
            return 0;
        }
    }

    printf("CORRECT\n");
    return 0;
}

D题 海盗分金币(加强版):

点击跳转

核心结论

多玩家Nim游戏。先计算 S = m_1 \oplus m_2 \oplus \cdots \oplus m_n。

胜负判定

  1. 一般情况:

  2. 特殊情况(所有堆都是1):

    • 否则:第 (n \bmod k) 位玩家赢

正确性

复杂度

代码如下: ```cpp line-numbers #include <iostream> #include <string> using namespace std; int main() { // 读取第一行的所有玩家名字 string line; getline(cin, line); // 读取堆数n int n; cin >> n; // 计算异或和 尼姆游戏核心逻辑 long long xor_sum = 0; for (int i = 0; i < n; i++) { long long m; cin >> m; xor_sum ^= m; } // 提取第一个玩家名字 find函数版 // 找到第一个空格的位置 size_t first_space = line.find(' '); // 截取从开头到第一个空格的子串 即为第一个玩家名字 string first_player = line.substr(0, first_space); // 输出结果 if (xor_sum == 0) { // 提取第二个玩家名字 从第一个空格后开始 到第二个空格结束 size_t second_space = line.find(' ', first_space + 1); string second_player = line.substr(first_space + 1, second_space - first_space - 1); cout << second_player << endl; } else { cout << first_player << endl; } return 0; } ``` ## E题 Dabi-攒1: [点击跳转](https://www.luogu.com.cn/problem/T718232?contestId=302095) ### 问题分析 有 n 个“攒”,可以分成若干组,每组消耗 1、2、3 或 4 个攒,对应伤害为 1、2、3、5。总伤害为各组伤害的乘积,需要最大化这个乘积。 ### 核心思路 这是一个**整数拆分求最大乘积**问题。通过分析不同拆分的价值: - 1个攒:伤害1 → 效率1.00 - 2个攒:伤害2 → 效率1.00 - 3个攒:伤害3 → 效率1.00 - 4个攒:伤害5 → 效率1.25 4个攒一组的效率最高(1.25),应尽可能多用。但需要正确处理剩余攒数。 ### 贪心策略 设 $n = 4k + r$,其中 $0 \le r < 4$。 1. **$r = 0$**:全部用4个攒一组,$S = 5^k
  1. r = 1:将一个4与这个1组合成2+3(伤害6),剩余全用4,S = 5^{k-1} \times 6
  2. r = 2:全用4,最后加一个2,S = 5^k \times 2
  3. r = 3:全用4,最后加一个3,S = 5^k \times 3

策略正确性证明

关键证明:当 r = 1 时,用2+3(伤害6)比用4+1(伤害5)更优。

比较两种情况:

显然方案B更优。当 n \ge 5 时,总能进行这样的调整。

边界情况

计算实现

对于 n \le 105,最大 k = 26,5^{26} \times 6 \approx 8.9 \times 10^{18} < 2^{64},可用 unsigned long long 直接计算,无需高精度。

复杂度分析

对于 n \le 105,计算非常快。

代码如下:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n;
    cin >> n;

    if (n <= 0) {
        cout << 0;
        return 0;
    }

    // 小情况直接处理
    if (n == 1) { cout << 1; return 0; }
    if (n == 2) { cout << 2; return 0; }
    if (n == 3) { cout << 3; return 0; }
    if (n == 4) { cout << 5; return 0; }

    int cnt4 = n / 4;      // 4分组的数量
    int r = n % 4;         // 余数
    unsigned long long ans = 1;

    // 快速计算 5^cnt4
    for (int i = 0; i < cnt4; i++) {
        ans *= 5;
    }

    // 根据余数调整
    if (r == 1) {
        ans = ans / 5 * 6;  // 把一个4换成2+3
    } else if (r == 2) {
        ans *= 2;
    } else if (r == 3) {
        ans *= 3;
    }
    // r==0 不用调整

    cout << ans;
    return 0;
}

F题 Dabi-攒2:

点击跳转

与攒1的区别

数据范围从 n \le 105 扩大到 n \le 1005,导致结果过大,必须使用高精度。

高精度需求分析

高精度实现要点

1. 数据结构

// 使用数组存储大数(倒序存储,低位在前)
int digits[1000];  // 每位数字,digits[0]是个位
int len;           // 当前数字长度

2. 核心操作:高精度乘低精度

// 将当前大数乘以一个小整数x(2≤x≤6)
void multiply(int x) {
    int carry = 0;
    for (int i = 0; i < len; i++) {
        carry += digits[i] * x;
        digits[i] = carry % 10;
        carry /= 10;
    }
    while (carry) {
        digits[len++] = carry % 10;
        carry /= 10;
    }
}

3. 计算流程

// 计算5^k
digits[0] = 1; len = 1;  // 初始化为1
for (int i = 0; i < k; i++) {
    multiply(5);
}

// 根据余数调整
if (r == 1) {
    multiply(6);
} else if (r == 2) {
    multiply(2);
} else if (r == 3) {
    multiply(3);
}
// r==0 保持不变

4. 输出

// 逆序输出(高位在前)
for (int i = len-1; i >= 0; i--) {
    cout << digits[i];
}

时间复杂度

代码如下:

#include <iostream>
#include <cstring>
using namespace std;

// 高精度整数类(简化版)
struct BigInt {
    int digits[1000]; // 存储数字,digits[0]是个位
    int len;          // 当前数字的长度

    BigInt() {
        memset(digits, 0, sizeof(digits));
        len = 1; // 初始化为数字0,但我们会重新初始化为1
    }

    // 初始化为1(用于计算5^0)
    void init() {
        memset(digits, 0, sizeof(digits));
        digits[0] = 1;
        len = 1;
    }

    // 高精度乘低精度
    void multiply(int x) {
        int carry = 0;
        for (int i = 0; i < len; i++) {
            carry += digits[i] * x;
            digits[i] = carry % 10;
            carry /= 10;
        }
        while (carry) {
            digits[len++] = carry % 10;
            carry /= 10;
        }
    }

    // 输出
    void print() {
        for (int i = len - 1; i >= 0; i--) {
            cout << digits[i];
        }
        cout << endl;
    }
};

int main() {
    int n;
    cin >> n;

    // n≤4的直接情况
    if (n == 1) { cout << 1; return 0; }
    if (n == 2) { cout << 2; return 0; }
    if (n == 3) { cout << 3; return 0; }
    if (n == 4) { cout << 5; return 0; }

    // 贪心策略:优先用4
    int k = n / 4;  // 4分组的数量
    int r = n % 4;  // 余数

    BigInt ans;
    ans.init(); // 初始化为1

    // 计算5^k
    for (int i = 0; i < k; i++) {
        ans.multiply(5);
    }

    // 根据余数调整
    if (r == 1) {
        // 把一个4换成2+3,即乘以6/5
        // 先除以5:这里直接重新计算更简单
        ans.init();
        for (int i = 0; i < k - 1; i++) {
            ans.multiply(5);
        }
        ans.multiply(6);
    } else if (r == 2) {
        ans.multiply(2);
    } else if (r == 3) {
        ans.multiply(3);
    }
    // r==0 保持不变

    ans.print();
    return 0;
}