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 ...
随机推荐
- 企业级 Web 开发的挑战
本文翻译自土牛Halil ibrahim Kalkan的<Mastering ABP Framework>,是系列翻译的起头,适合ABP开发人员或者想对ABP框架进行深入演进的准架构师. ...
- 【面试普通人VS高手系列】说说缓存雪崩和缓存穿透的理解,以及如何避免?
听说10个人去互联网公司面试,有9个人会被问到缓存雪崩和缓存穿透的问题. 听说,这9个人里面,至少有8个人回答得不完整. 而这8个人里面,全都是在网上找的各种面试资料去应付的,并没有真正理解. 当然, ...
- 太极限了,JDK的这个BUG都能被我踩到
hello,大家好呀,我是小楼. 之前遇到个文件监听变更的问题,刚好这周末有空研究了一番,整理出来分享给大家. 从一次故障说起 我们还是从故障说起,这样更加贴近实际,也能让大家更快速理解背景. 有一个 ...
- Yapi Docker 部署
参考 https://github.com/Ryan-Miao/docker-yapi , 并使用该代码的脚本构建yapi image. 部署mongodb docker run \ --name m ...
- 中国电子云数据库 Mesh 项目 DBPack 的实践
作者:刘晓敏 2022 年 4 月,中国电子云开源了其云原生数据库 Mesh 项目 DBPack.该项目的诞生,旨在解决用户上云过程中面临的一些技术难点,诸如分布式事务.分库分表等.由于它数据库 Me ...
- swagger在线api文档搭建指南,用于线上合适么?
在上一篇文章中,我们讲解了什么是 api,什么是 sdk: https://www.cnblogs.com/tanshaoshenghao/p/16217608.html 今天将来到我们万丈高楼平地起 ...
- springMvc和Hibernate集成实现用户添加
源码:http://pan.baidu.com/s/1i4xVLE9(百度云) 步骤:一.创建数据库(mysql) 二.导入相应jar包(注意不同数据库jdbc.jar包)配置web.xml.spri ...
- Spring Security之用户名+密码登录
自定义用户认证逻辑 处理用户信息获取逻辑 实现UserDetailsService接口 @Service public class MyUserDetailsService implements Us ...
- linux篇-linux LAMP yum版安装
LAMP(linux.apache.mysql.php),是四个套件的合成,简单讲就是要把php运行在linux上,需要依赖apache和mysql数据库. 1 准备好一个linux系统(centos ...
- 使用 Postman 实现 API 自动化测试
背景介绍 相信大部分开发人员和测试人员对 postman 都十分熟悉,对于开发人员和测试人员而言,使用 postman 来编写和保存测试用例会是一种比较方便和熟悉的方式.但 postman 本身是一个 ...