Educational Codeforces Round 129 (Rated for Div. 2) A-D

比赛链接

A

题解

知识点:贪心。

先手的一方拥有大于等于对方最大牌的牌即可获胜,所以考虑取两组牌各自的最大值进行比较。

时间复杂度 O(n)

空间复杂度 O(1)

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool solve() {
int a = 0, b = 0;
int n;
cin >> n;
for (int i = 0, tmp = 0;i < n;i++) cin >> tmp, a = max(a, tmp);
int m;
cin >> m;
for (int j = 0, tmp = 0;j < m;j++) cin >> tmp, b = max(b, tmp);
if (a >= b) cout << "Alice" << '\n';
else cout << "Bob" << '\n';
if (b >= a) cout << "Bob" << '\n';
else cout << "Alice" << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

B

题解

知识点:模拟,数学。

注意到每次取的牌顺序不变放入排堆尾部,因此是一个循环,于是交换总和除以牌总数的余数 sum%n 即等效交换次数,而对应的数组元素即交换后的牌顶元素。

时间复杂度 O(n)

空降复杂度 O(n)

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[200007];
bool solve() {
int n;
cin >> n;
for (int i = 0;i < n;i++) cin >> a[i];
int m;
ll sum = 0;
cin >> m;
for (int i = 0, tmp = 0;i < m;i++) cin >> tmp, sum += tmp;
cout << a[sum % n] << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

C

题解

知识点:模拟,排序。

注意到 n 最大只有 100 ,允许交换操作最大能到 104 而且不需要最小化交换次数,显然可以考虑先将数组 a 进行选择排序(相等元素是不需要交换的,保险点防止炸 104 ),并在交换过程中同时交换 b 相同位置的元素。然后对数组 b 用选择排序进行检查,并且交换只能在相同位置数组 a 元素相等时才行,如果某次交换因为 a 限制不可操作,则一定判失败;否则记录所有结果输出即可。

时间复杂度 O(n2)

空间复杂度 O(n)

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[107], b[107];
bool solve() {
vector<pair<int, int>> v;
int n;
cin >> n;
for (int i = 0;i < n;i++)cin >> a[i];
for (int i = 0;i < n;i++)cin >> b[i];
for (int i = 0;i < n - 1;i++) {
int minpos = i;
for (int j = i + 1;j < n;j++) {
minpos = a[minpos] > a[j] ? j : minpos;
}
if (i != minpos) swap(a[i], a[minpos]), swap(b[i], b[minpos]), v.push_back({ i,minpos });
}
for (int i = 0;i < n - 1;i++) {
int minpos = i;
for (int j = i + 1;j < n;j++) {
minpos = b[minpos] > b[j] ? j : minpos;
}
if (i != minpos) {
if (a[i] == a[minpos]) swap(b[i], b[minpos]), v.push_back({ i,minpos });
else return false;
}
}
cout << v.size() << '\n';
for (int i = 0;i < v.size();i++) {
cout << v[i].first + 1 << ' ' << v[i].second + 1 << '\n';
}
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

D

题解

知识点:搜索。

首先注意到每次贪心地取最大数位去乘是不可行的,比如有 5×5>9×2 如果先选 9 后只有 2 那就没有选两次 5 更优。

因此,考虑用dfs枚举每种可能,当然直接枚举会tle,有一个重要剪枝 if (step + n - len >= ans) return; 意思是已走步数 step 加上至少还需要的步数 n-len 如果大于等于当前答案 ans 那这条分支就是无用的,这个叉掉很多分支。

当然,也可以用bfs记忆化搜索,直接bfs也会超时,还可能炸空间qwq。

时间复杂度 O(8n)

空间复杂度 O(n)

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, x, ans = 100;
void dfs(ll cur = x, ll step = 0) {
bool vis[10] = { 0 };
ll tmp = cur, len = 0;
while (tmp) {
vis[tmp % 10] = 1;
tmp /= 10;
len++;
}
if (step + n - len >= ans) return;
if (n == len) {
ans = step;
return;
}
for (int i = 9;i >= 2;i--) if (vis[i]) dfs(cur * i, step + 1);
}
bool solve() {
cin >> n >> x;
dfs();
if (ans >= 100) return false;
cout << ans << '\n';
return true;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}
posted @   空白菌  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示