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. Blazor Bootstrap 组件库地理定位/移动距离追踪组件介绍

    地理定位/移动距离追踪组件 通过浏览器 API 获取定位信息 DEMO https://www.blazor.zone/geolocations 小提示 注意: 出于安全考虑,当网页请求获取用户位置信 ...

  2. 从压测碰到的诡异断连问题聊聊Nginx的连接管理

    本文主要分享一个在压测Nginx反向代理服务过程中碰到的连接异常断开问题,包括问题的定位与复现,最后由这个实际问题引申聊一下Nginx的连接管理. 本博客已迁移至CatBro's Blog,那是我自己 ...

  3. OpenHarmony 3.1 Beta 样例:使用分布式菜单创建点餐神器

    (以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点) 刘丽红 随着社会的进步与发展,科技手段的推陈出新,餐饮行业也在寻求新的突破与变革,手机扫描二维码点餐系统已经成为餐饮行 ...

  4. oracle split 以及 简单json解析存储过程

    BEGIN; 由于之前工作上需要在oracle中做split功能以及json格分解.然后经过一番google和优化整合,最后整理到一个存储过程包中,易于管理,代码如下: 1.包定义: CREATE O ...

  5. jmeter并发设置的原理

    目录 简介 广义并发 绝对并发 简介 ​ 性能测试过程中是否需要进行同步定时器的设置,需要根据实际情况来考虑. ​ 举个栗子来讲是我们的双十一秒杀活动,这时候就必须实现请求数量达到一定数量后同时向服务 ...

  6. 【转】python代码优化常见技巧

    https://blog.csdn.net/egefcxzo3ha1x4/article/details/97844631

  7. jfinal极速开发

    下载jfinal项目,上面都配置好了不用自己新建从头配置.https://jfinal.com/ idea打开项目 配置数据库 resources目录下demo-config-dev.txt # co ...

  8. python3修改HTMLTestRunner,生成有截图的测试报告,并发送测试邮件(二)

    3. 如何将第一步得到的地址和名称 输入 进第二步里的表格中呢... 用上述查找元素的方法,发现HTMLTestRunner.py中REPORT_TEST_WITH_OUTPUT_TMPL是用来输出测 ...

  9. js实时查询,为空提示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 面试官:ElasticSearch是什么,它有什么特性与使用场景?

    哈喽!大家好,我是小奇,一位热爱分享的程序员 小奇打算以轻松幽默的对话方式来分享一些技术,如果你觉得通过小奇的文章学到了东西,那就给小奇一个赞吧 文章持续更新 一.前言 书接上回,我本以为我跟面试我的 ...