Educational Codeforces Round 129 (Rated for Div. 2) A-D
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的更多相关文章
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- 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 < ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
随机推荐
- Go能实现AOP吗?
hello~大家好,我是小楼,今天分享的话题是Go是否能实现AOP? 背景 写Java的同学来写Go就特别喜欢将两者进行对比,就经常看到技术群里讨论,比如Go能不能实现Java那样的AOP啊?Go写个 ...
- 《Mybatis 手撸专栏》第6章:数据源池化技术实现
作者:小傅哥 博客:https://bugstack.cn - 手写Mybatis系列文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 码农,只会做不会说? 你有发现吗,其实很大一部分码农 ...
- Linux服务器如何识别移动硬盘?
序言 通常我们使用的移动硬盘或U盘一般都是ntfs或fat32的文件系统,常跟服务器打交道的小伙伴,会经常遇到把移动硬盘或U盘上的数据拷贝到Linux服务器上.绝大多数Linux发行版内核支持fat3 ...
- 干货 | Nginx 配置文件详解
一个执着于技术的公众号 前言 在前面章节中,我们介绍了nginx是什么.如何编译安装nginx及如何彻底卸载nginx软件. 干货|给小白的 Nginx 10分钟入门指南 Nginx编译安装及常用命令 ...
- 干货|SQL语句大全,所有的SQL都在这里了(建议收藏)
一个执着于技术的公众号 一.基础 1.登录数据库 mysql -uroot -p123123 2.创建数据库 create database <数据库名> 3.删除数据库 drop dat ...
- 2020年DevOps工程师入门指南
DevOps兴起于2010年代,到现在DevOps已经在行业中拥有了一席之地,并在继续发展壮大. 有兴趣成为一名DevOps工程师吗?如果想要成为一名DevOps工程师,需要做到以下五点: 要有开发者 ...
- Docker系列教程02-操作Docker容器
简介 通过前面的学习,相信您已经对镜像有所了解,是时候学习容器了. 容器是Docker的另一个核心概念.简单来说,容器是镜像的一个运行实例.正如从虚拟机模板上启动VM一样,用户也同样可以从单个镜像上启 ...
- grafana v8.0+ 隐藏表格字段
Select panel title → Inspect → Panel JSON Set "type" to "table-old" Apply The vi ...
- C++进阶-3-6-map/multimap容器
C++进阶-3-6-map/multimap容器 1 #include<iostream> 2 #include<map> 3 using namespace std; 4 5 ...
- scrapy框架第二天
1.scrapy数据分析 2.scrapy持久化存储 3.全站数据爬取 4.请求传参 + 五大核心组件 - 创建scrapy工程 scrapy startproject ProName - 切换 ...