题解 | CVZOI Round 2 A-F题 题解
Charles0324 · · 个人记录
这里是 2025ZP-早六五班-出题组 举办的 CVZOI Round 2 的A-G题题解
废话不多说,直接看题解
A题 通用签到题:
点击跳转
代码如下:
#include<iostream>
using namespace std;
int main(){
cout << "I love RDFZ!";
return 0;
}
很简单,不多说了。
B题 白昼流星:
点击跳转
很简单的贪心题目,只要认真听讲,每次选择结束时间最早的即可。
因为所有流星幸运值相同,总幸运值 = 选中数量 × V。
- 按
l_i 排序 - 遍历,若
t_i \ge 上一个结束时间,则选 - 答案 = 选中数 × V
时间复杂度
代码如下:
#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。
检查顺序:
- 长度检查:密码长度是否在
[L, R] 范围内。 - 字符类型检查:
- 遍历字符串,统计是否出现大写字母、小写字母、数字。
- 同时检查是否有其他非法字符(非字母数字)。
- 连续字符检查:
- 遍历每连续三个字符:
- 判断是否完全相同(
aaa型)。 - 判断 ASCII 码是否严格连续递增 1(
abc、123型)。 - 判断 ASCII 码是否严格连续递减 1(
cba、ZYX型)。
- 判断是否完全相同(
- 注意:只有同类型字符(同为数字/大写/小写)才可能构成连续递增/递减。
- 遍历每连续三个字符:
特殊说明:
- 条件4 中的“连续”指三个字符在字符串中位置连续。
- 数字
789、大写XYZ、小写abc都是非法连续。 - 跨类型如
ab1、9AB不会构成非法连续。
代码如下:
#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游戏。先计算
胜负判定
-
一般情况:
-
-
特殊情况(所有堆都是1):
-
- 否则:第
(n \bmod k) 位玩家赢
-
正确性
-
-
S = 0$:先手任何操作都会使 $S \neq 0$,后手可恢复 $S=0
复杂度
r = 1 :将一个4与这个1组合成2+3(伤害6),剩余全用4,S = 5^{k-1} \times 6 r = 2 :全用4,最后加一个2,S = 5^k \times 2 r = 3 :全用4,最后加一个3,S = 5^k \times 3
策略正确性证明
关键证明:当
比较两种情况:
- 方案A:一个4(伤害5)+一个1(伤害1)→ 总伤害5
- 方案B:一个2(伤害2)+一个3(伤害3)→ 总伤害6
显然方案B更优。当
边界情况
计算实现
对于 unsigned long long 直接计算,无需高精度。
复杂度分析
- 时间复杂度:
O(k) ,其中k = n/4 - 空间复杂度:
O(1)
对于
代码如下:
#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 = 1005 ,r=1 (余数1) -
k = 251$,需要计算 $5^{250} \times 6 -
- 远超
unsigned long long范围
高精度实现要点
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];
}
时间复杂度
- 高精度乘法:每次
O(L) ,L 为当前数字长度 - 总操作:
O(k \times L_{avg}) ,k \le 251 ,L_{avg} \le 176 - 总计算量约
4.4 \times 10^4 次基本运算,完全可行
代码如下:
#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;
}