第十四届蓝桥杯国赛 C/C++ 大学 B 组
试题 A: 子 2023
本题总分:
【问题描述】
小蓝在黑板上连续写下从
小蓝想知道
提示,以下是
注意以下是不满足条件的子序列,虽然包含了
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分
【解释】
当遇到字符 ‘2’ 的时候字符串 “2” 的数量+1,字符串 “202” 的数量加上字符串 “20” 的数量
当遇到字符 ‘0’ 的时候字符串 “20” 的数量加上字符串 “2” 的数量
当遇到字符 ‘3’ 的时候字符串 “2023” 的数量加上字符串 “202” 的数量
最后 “2023” 的数量就是答案
点击查看代码
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 1e6 + 10; int n, ans, cnt; bool st[N]; void solve() { string s; for(int i = 1; i <= 2023; i ++) s += to_string(i); vector<int> f(5, 0); for(int i = 0; i < s.size(); i ++) { if(s[i] == '2') { f[0] ++; f[2] += f[1]; } else if(s[i] == '0') f[1] += f[0]; else if(s[i] == '3') f[3] += f[2]; } cout << f[3] << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); // init(); int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; } //5484660609
【答案】5484660609
试题 B: 双子数
本题总分:
【问题描述】
若一个正整数
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
【解释】
先用欧拉筛求出
点击查看代码
#include <bits/stdc++.h> #define int __int128 using namespace std; const int N = 1e7 + 10; int n, ans, cnt; int prime[N]; bool st[N]; void init() { for(int i = 2; i < N; i ++) { if(!st[i]) prime[cnt ++] = i; for(int j = 0; j < cnt&&prime[j] * i < N; j ++) { st[prime[j] * i] = true; if(i % prime[j] == 0) break; } } } void solve() { int ans = 0; for(int i = 0; i < cnt; i ++) for(int j = i + 1; j < cnt; j ++) { int a = prime[i], b = prime[j]; if(a * a * b * b > 23333333333333) break; if(a * a * b * b < 2333) continue; ans ++; } cout << (long long)ans << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); init(); int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; } //947293
【答案】947293
试题 C: 班级活动
时间限制:
【问题描述】
小明的老师准备组织一次班级活动。班上一共有
老师希望通过更改若干名同学的
【输入格式】
输入共
第一行为一个正整数
第二行为
【输出格式】
输出共
【样例输入】
4
1 2 2 3
【样例输出】
1
【样例说明】
仅需要把
【评测用例规模与约定】
对于
对于
【解释】
把题目捋清楚就很容易实现了
1、先用
2、数量大于等于
3、
点击查看代码
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr) #define int long long #define fi first #define se second using namespace std; const int N = 2e5 + 10; int a[N], n, s1, s2; void solve() { cin >> n; unordered_map<int, int> mp; for(int i = 1; i <= n; i ++) { int x; cin >> x; mp[x] ++; } for(auto x : mp) { if(x.se >= 2) s1 += x.se - 2; else s2 += x.se; } if(s1 >= s2) cout << s1 << '\n'; else cout << s1 + (s2 - s1) / 2 << '\n'; } signed main() { IOS; int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; }
试题 D: 合并数列
时间限制:
【问题描述】
小明发现有很多方案可以把一个很大的正整数拆成若干正整数的和。他采取了其中两种方案,分别将他们列为两个数组
定义一次合并操作可以将某数组内相邻的两个数合并为一个新数,新数的值是原来两个数的和。小明想通过若干次合并操作将两个数组变成一模一样,即
【输入格式】
输入共
第一行为两个正整数
第二行为
第三行为
【输出格式】
输出共
【样例输入】
4 3
1 2 3 4
1 5 4
【样例输出】
1
【样例说明】
只需要将
【评测用例规模与约定】
对于
对于
【解释】
其实就是一个贪心题
1、对比两个数组最左边的数字,哪边小就合并哪一边
2、如果两个数组最左边的数字相等就出队
双端队列
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr) #define int long long #define fi first #define se second #define fr front #define pb push_back #define pf pop_front using namespace std; const int N = 2e5 + 10; int n, m, ans; void solve() { deque<int> q1, q2; cin >> n >> m; for(int i = 0; i < n; i ++) { int x; cin >> x; q1.pb(x); } for(int i = 0; i < m; i ++) { int x; cin >> x; q2.pb(x); } while(!q1.empty()) { if(q1.fr() == q2.fr()) { q1.pf(); q2.pf(); } else if(q1.fr() > q1.fr()) { q2[1] += q2[0]; q2.pf(); ans ++; } else { q1[1] += q1[0]; q1.pf(); ans ++; } } cout << ans << '\n'; } signed main() { IOS; int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; }
数组
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr) #define int long long #define fi first #define se second #define fr front #define pb push_back #define pf pop_front using namespace std; const int N = 2e5 + 10; int n, m, ans, sa, sb; int a[N], b[N]; void solve() { cin >> n >> m; for(int i = 1; i <= n; i ++) cin >> a[i]; for(int i = 1; i <= m; i ++) cin >> b[i]; int i = 1, j = 1; while(i <= n + 1&&j <= m + 1) { if(sa == sb) sa += a[i ++], sb += b[j ++]; else if(sa < sb) ans ++, sa += a[i ++]; else ans ++, sb += b[j ++]; } cout << ans << '\n'; } signed main() { IOS; int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; }
试题 E: 数三角
时间限制:
【问题描述】
小明在二维坐标系中放置了
【输入格式】
输入共
第一行为一个正整数
后面
【输出格式】
输出共
【样例输入】
5
1 4
1 0
2 1
1 2
0 1
【样例输出】
4
【样例说明】
一共有
【评测用例规模与约定】
对于
对于
【解释】
样例应该输出5,比赛时勘误了,{1, 3, 5}也是可以的
1、枚举每个点,计算其它点与该点的距离,距离相同的两条边可以构成等腰三角型
2、注意要考虑共线问题
3、其实这个方法不是很行,等边三角形会被重复计算,正解是什么不是很清楚
点击查看代码
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr) #define int long long #define fi first #define se second #define fr front #define pb push_back #define pf pop_front using namespace std; const int N = 3010, M = 2e6 + 10; int n, m, ans; int x[N], y[N]; int st[N][N]; int s[M]; int dis(int i, int j) { int a = x[i] - x[j], b = y[i] - y[j]; return a * a + b * b; } void solve() { cin >> n; for(int i = 0; i < n; i ++) { cin >> x[i] >> y[i]; st[x[i] + 1500][y[i] + 1500] = 1; } int ans = 0; for(int i = 0; i < n; i ++) { for(int j = 0; j < n; j ++) { if(i == j) continue; ans += s[dis(i, j)]; s[dis(i, j)] ++; if(st[2 * x[i] - x[j] + 1500][2 * y[i] - y[j] + 1500]) m ++; } for(int j = 0; j < n; j ++) { if(i == j) continue; s[dis(i, j)] = 0; } } cout << ans - m / 2 << '\n'; } signed main() { IOS; int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; }
试题 F: 删边问题
时间限制:
【问题描述】
给定一个包含
你可以从
对于一种合法的删除方案,我们假设
【输入格式】
第一行包含两个整数
第二行包含
以下
【输出格式】
一个整数代表最小的差值。如果不存在合法的删除方案,输出
【样例输入】
4 4
10 20 30 40
1 2
2 1
2 3
4 3
【样例输出】
20
【样例说明】
由于
删除
删除
【评测用例规模与约定】
对于
对于另外
对于
【解释】
会缩点的话这题就不成问题了!但是我不会提供点思路
1、用Tarjan算法将环缩成一个点,缩点后将会的到一棵树
2、统计树每个节点的字节点和(
3、枚举每个点,答案就是每个点的
试题 G: AB 路线
时间限制:
【问题描述】
有一个由
由于特殊的原因,小蓝的路线必须先走
请你计算小蓝最少需要走多少步,才能到达右下角方格?
注意路线经过的格子数不必一定是
例如
以下
【输入格式】
第一行包含三个整数
以下
【输出格式】
一个整数,代表最少步数。如果无法到达右下角,输出
【样例输入】
4 4 2
AAAB
ABAB
BBAB
BAAA
【样例输出】
8
【样例说明】
每一步方向如下:下右下右上右下下;路线序列:
【评测用例规模与约定】
对于
对于另
对于
【解释】
经典的
试题 H: 抓娃娃
时间限制:
【问题描述】
小明拿了
【输入格式】
输入共
第一行为两个正整数
后面
后面
【输出格式】
输出共
【样例输入】
3 2
1 2
1 3
3 4
1 4
2 4
【样例输出】
3
2
【评测用例规模与约定】
对于
对于
【解释】
题目勘误了,样例最后一行应该是
1、其实题目保证了
2、因为涉及了小数,给每个数字都乘以
点击查看代码
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(nullptr),cout.tie(nullptr) #define int long long #define fi first #define se second #define fr front #define pb push_back #define pf pop_front using namespace std; const int N = 2e6 + 10; int n, m, ans; int s[N << 1]; void solve() { cin >> n >> m; for(int i = 1; i <= n; i ++) { int l, r; cin >> l >> r; s[l + r] ++; } for(int i = 1; i < N * 2; i ++) s[i] += s[i - 1]; while(m --) { int l, r; cin >> l >> r; cout << s[2 * r] - s[2 * l - 1] << '\n'; } } signed main() { IOS; int _ = 1; // cin >> _; while(_ --) solve(); return _ ^ _; }
试题 I: 拼数字
时间限制:
【问题描述】
小蓝要用
【输入格式】
两个整数
【输出格式】
一个
【样例输入】
2 8
【样例输出】
2233333333
【评测用例规模与约定】
对于
对于
对于
对于
【解释】
不会做啊,痛苦!!!
试题 J: 逃跑
时间限制:
【问题描述】
小明所在星系有
为了在星际战争到来时逃到其他星系,小明在根结点设置了逃离用的传送门。每个星球的人只需要一直往父结点星球移动就可以抵达根结点。为了方便各个星球的人去往根结点,小明将其中
然而,因为技术问题,向跳板星球的跳跃并不一定成功,每一次跳跃都有
为了衡量移动效率,小明想知道,如果一个人在这
【输入格式】
输入共
后面
最后一行,共
【输出格式】
一行,一个浮点数,表示答案(请保留两位小数)。
【样例输入】
4 1 0.2
1 2
2 3
3 4
2
【样例输出】
1.30
【样例说明】
从
从
从
从
所以期望时间为
【评测用例规模与约定】
对于
对于
【解释】
也不会做啊,痛苦!!!
本文作者:chfychin
本文链接:https://www.cnblogs.com/chfychin/p/17759246.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步