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

A

题目

https://codeforces.com/contest/1681/problem/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

题目

https://codeforces.com/contest/1681/problem/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

题目

https://codeforces.com/contest/1681/problem/C

题解

思路

知识点:模拟,排序。

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

时间复杂度 \(O(n^2)\)

空间复杂度 \(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

题目

https://codeforces.com/contest/1681/problem/D

题解

思路

知识点:搜索,暴力。

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

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

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

时间复杂度 \(O(8^n)\)

空间复杂度 \(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;
}

Educational Codeforces Round 129 (Rated for Div. 2) A-D的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

随机推荐

  1. 2021.07.23 P3275 糖果(差分约束)

    2021.07.23 P3275 糖果(差分约束) [P3275 SCOI2011]糖果 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.为了满足更多更多约束条件,合适地 ...

  2. transition 动画过渡

    1. html 结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. MySQL进阶之表的增删改查

    我的小站 修改表名 ALTER TABLE student RENAME TO stu; TO可以省略. ALTER TABLE 旧表名 RENAME 新表名; 此语句可以修改表的名称,其实一般我们在 ...

  4. 大一/初学者学C语言前必看!!!(建议收藏)

    目录 数据类型 常量.变量 数组 字符串.转义字符 选择语句 循环语句 函数 操作符 结构体 指针 神秘的学习资料基地​jq.qq.com/?_wv=1027&k=5kWJsY1z 一.数据类 ...

  5. 类变量_main方法_代码块

    需要解决的问题: 统计一共创建了多少个对象 类变量(静态变量) 被所有对象共享 可以通过 类名(推荐)|对象名.变量名 方式来访问 main main 方法 是虚拟机在调用  args 传值灵活 结果 ...

  6. 一篇文章教你搞懂日志采集利器 Filebeat

    关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 本文使用的Filebeat是7.7.0的版本,文章将从如下几个方面说明: Filebeat是什 ...

  7. 面试官:我把数据库部署在Docker容器内,你觉得如何?

    开源Linux 一个执着于技术的公众号 上一篇:CentOS 7上搭建Zabbix4.0 近2年Docker非常的火热,各位开发者恨不得把所有的应用.软件都部署在Docker容器中,但是您确定也要把数 ...

  8. HamsterBear Linux Low Res ADC按键驱动的适配 + LVGL button移植

    HamsterBear lradc按键驱动的适配 平台 - F1C200s Linux版本 - 5.17.2 ADC按键 - 4 KEY tablet 驱动程序位于主线内核: drivers/inpu ...

  9. MPLS基础与工作原理

    MPLS Fundamental History of WAN Protocol 1970年代之前 第一个 WAN 用于将办公室与终端连接到大型机和小型计算机系统. 它是从办公室到数据中心的点对点连接 ...

  10. monit 命令详解(monit)

    monit是Monit软件的主操作控制命令. 语法 monit [options]+ [command] 选项(options) -c file 指定要使用的配置文件 -d n 每间隔多少秒运行一次M ...