Codeforces Round #671 (Div. 2)
比赛链接:https://codeforces.com/contest/1419
A. Digit Game
题意
给出一个 $n$ 位数,游戏规则如下:
- 1-indexed
- Raze标记奇数位
- Breach标记偶数位
- 如果最后剩下的一位未标记位上的数字为奇数,Raze胜,为偶数,Breach胜
问Raze先手能否胜利。
题解
根据 $n$ 的奇偶性可得最后的未标记位的奇偶性,然后判断该奇偶性的位置上是否存在奇数或偶数即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
bool Raze = true;
if (s.size() & 1) {
Raze = false;
for (int i = 0; i < int(s.size()); i += 2) {
if ((s[i] - '0') & 1)
Raze = true;
}
} else {
Raze = true;
for (int i = 1; i < int(s.size()); i += 2) {
if ((s[i] - '0') % 2 == 0)
Raze = false;
}
}
cout << (Raze ? 1 : 2) << "\n";
}
return 0;
}
B. Stairs
题意
共有 $n$ 个边长为 $1$ 的小正方形可用,问能拼成多少个不同的可以被完美覆盖的阶梯。
边长为1、3、7的阶梯都可以被完美覆盖,方式如下:
题解
完美阶梯的边长是以 $2$ 的幂次递增的,当前阶梯所用小正方形块数 = 上一个所用块数 X 2 + 递增步长2。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
int ans = 0;
long long pre = 0, now = 0;
for (long long i = 1; true; i *= 2) {
now = 2 * pre + i * i;
x -= now;
pre = now;
if (x >= 0) {
++ans;
} else {
cout << ans << "\n";
break;
}
}
}
return 0;
}
C. Killjoy
题意
有一个初始时感染的账号,Rating为 $x$,感染规则如下:
- Rating相同的账号相互感染
- 可以举办比赛任意加减Rating,但应保证总的变化之和为零,即比赛前后总Rating和不变
- 初始账号不能参加比赛
给出 $n$ 个账号的Rating,计算感染完所有账号至少要举办多少场比赛。
题解
如果总Rating之和为 $x$ 的倍数:
- 如果均为 $x$,无需举办
- 否则举办一场将总Rating均分即可
如果总Rating之和不为 $x$ 的倍数:
- 如果有 $x$,感染 $x$ 后举办一场比赛将其他账号都变为 $x$
- 否则需要先举办一场得到一个 $x$,之后同上
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> x;
vector<int> a(n);
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % n == 0 and sum / n == x) {
if (all_of(a.begin(), a.end(), [&](int y) { return y == x; }))
cout << 0 << "\n";
else
cout << 1 << "\n";
} else {
if (any_of(a.begin(), a.end(), [&](int y) { return y == x; }))
cout << 1 << "\n";
else
cout << 2 << "\n";
}
}
return 0;
}
D2. Sage's Birthday (hard version)
题意
给出 $n$ 个数,试重新排列使得相邻两个数比自己大的数的个数最多。
题解一
先将较小的一半放在中间,然后upper_bound查找每个数左边相邻的数,剩下的数依次填补空位即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
multiset<int> st(a.begin(), a.end());
vector<int> v(n);
for (int i = 1, j = 0; i < n; i += 2) {
v[i] = a[j];
st.erase(st.find(a[j]));
++j;
}
for (int i = 1; i < n; i += 2) {
auto it = st.upper_bound(v[i]);
if (it != st.end()) {
v[i - 1] = *it;
st.erase(it);
}
}
for (int i = 0; i < n; i++) {
if (v[i] == 0) {
v[i] = *st.begin();
st.erase(st.begin());
}
}
int ans = 0;
for (int i = 1; i + 1 < n; i += 2) {
if (v[i - 1] > v[i] and v[i + 1] > v[i])
++ans;
}
cout << ans << "\n";
for (auto i : v) cout << i << ' ';
return 0;
}
题解二
先将较小的一半放在中间,然后将较大的一半放在两边。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
vector<int> v(n);
int cur = 0;
for (int i = 1; i < n; i += 2) {
v[i] = a[cur++];
}
for (int i = 0; i < n; i += 2) {
v[i] = a[cur++];
}
int ans = 0;
for (int i = 1; i + 1 < n; i += 2) {
if (v[i - 1] > v[i] and v[i] < v[i + 1])
++ans;
}
cout << ans << "\n";
for (auto i : v) cout << i << ' ';
return 0;
}
E. Decryption
题意
将一个合数所有大于 $1$ 的因子围成一个环,使得相邻互质的数最少。
题解
质因子分解和DFS,将DFS过程中最高位不为 $0$ 的质因子作为 $gcd$ 分组。
例如:
$30 = 2^1 \times 3^1 \times 5^1$
DFS过程如下:
一组($gcd = 5$):
$5 = 2^0 \times 3^0 \times 5^1$
二组($gcd = 3$):
$3 = 2^0 \times 3^1 \times 5^0$
$15 = 2^0 \times 3^1 \times 5^1$
三组($gcd = 2$):
$2 = 2^1 \times 3^0 \times 5^0$
$10 = 2^1 \times 3^0 \times 5^1$
$6 = 2^1 \times 3^1 \times 5^0$
$30 = 2^1 \times 3^1 \times 5^1$
发现不同组之间首尾互质(当然了),可以根据DFS的性质:后一组的最大数一定会覆盖前一组的任意一个数将每组反转,得到:
5
15 3
30 6 10 2
此时只有 $5$ 和 $2$ 互质,再根据DFS的性质将最后一组末尾两个数交换,最终得到:
5 15 3 30 6 2 10
此时也可以发现相邻互质的数的对数最少为 $0$ 或 $1$,之后模拟上述过程即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
//质因子分解
vector<int> p, e;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
p.push_back(i);
e.push_back(0);
while (n % i == 0) {
++e.back();
n /= i;
}
}
}
if (n > 1) {
p.push_back(n);
e.push_back(1);
}
//num低位至高位代表从小到大的质因子
vector<int> num(100);
function<int(void)> cal_num = [&]() {
int res = 1;
for (int i = 0; i < int(p.size()); i++) {
int mul = 1;
for (int j = 0; j < num[i]; j++) {
mul *= p[i];
}
res *= mul;
}
return res;
};
//对质因子的指数进行dfs
map<int, vector<int>> mp;
vector<int> order;
function<void(int, int)> dfs = [&](int dep, int top_dep) {
if (dep == int(p.size())) {
int x = cal_num();
if (x > 1) {
if (int(order.size()) == 0 or top_dep != order.back()) {
order.push_back(top_dep);
}
mp[order.back()].push_back(x);
}
return;
}
for (int i = 0; i <= e[dep]; i++) {
num[dep] = i;
if (i >= 1 and top_dep == -1)
dfs(dep + 1, dep);
else
dfs(dep + 1, top_dep);
}
};
dfs(0, -1);
//反转每组因子
vector<int> ans;
for (int i = 0; i < int(order.size()); i++) {
reverse(mp[order[i]].begin(), mp[order[i]].end());
for (auto j : mp[order[i]]) ans.push_back(j);
}
//交换最后一组的后两个元素
swap(ans.back(), ans[int(ans.size()) - 2]);
for (int i = 0; i < int(ans.size()); i++) {
cout << ans[i] << " \n"[i == int(ans.size()) - 1];
}
//最多有一对因子互质,即只有两个素因子的情况,也可以逐对判断
cout << (__gcd(ans[0], ans[1]) == 1) << "\n";
}
return 0;
}
Codeforces Round #671 (Div. 2)的更多相关文章
- Codeforces Round #671 (Div. 2) B. Stairs 难度1200
题目链接: Problem - 1419B - Codeforces 题目 题意 给x个格子,你可以用这x个格子去拼成楼梯 好的楼梯的要求如下: 1. 第n列有n个格子 2. 这个楼梯的所有格子可以被 ...
- Codeforces Round #671 (Div. 2) (A~E)
Link~ 题面差评,整场都在读题 A 根据奇偶性判断一下即可. #include<bits/stdc++.h> #define ll long long #define N #defin ...
- Codeforces Round #671 (Div. 2) B. Stairs (递推)
题意:一个台阶由一些单元格组成,如果一个高度为\(n\)的台阶中有\(n\)个不相邻的正方形(如图中的样例),就称这个台阶是"好台阶",现给你\(x\)个单元格,问最多能组成多少个 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- #2020征文-开发板# 用鸿蒙开发AI应用(一)硬件篇
目录: 前言 开发板简介 产品特色及功能 产品参数 各个主板功能简介 Hi3516DV300 芯片手册 前言鸿蒙2.0的系统刚开源出来,华为志在打造1+8+N万物互联的全场景智慧生活,不仅是国产操作系 ...
- 写给小白看的Mysql事务
1 为什么需要事务 在网上的很多资料里,其实没有很好的解释为什么我们需要事务.其实我们去学习一个东西之前,还是应该了解清楚这个东西为什么有用,硬生生的去记住事务的ACID特性.各种隔离级别个人认为没有 ...
- 使用vs code搭建Q#开发环境 (Mac)
Q# 是微软几年前发布的一门用于模拟量子编程的语言. 3年前我在当时风靡的博客网站 ITEYE 上发布过如何在windows上搭建其开发环境:Q#开发环境搭建.时过境迁,不但iteye不知何处去,连Q ...
- Spring中的@Valid 和 @Validated注解你用对了吗
1.概述 本文我们将重点介绍Spring中 @Valid和@Validated注解的区别 . 验证用户输入是否正确是我们应用程序中的常见功能.Spring提供了@Valid和@Validated两个注 ...
- 剑指offer 查找和排序的基本操作:查找排序算法大集合
重点 查找算法着重掌握:顺序查找.二分查找.哈希表查找.二叉排序树查找. 排序算法着重掌握:冒泡排序.插入排序.归并排序.快速排序. 顺序查找 算法说明 顺序查找适合于存储结构为顺序存储或链接存储的线 ...
- 如何实现CentOS服务器的扩容??
Linux的硬盘识别: 一般使用"fdisk -l"命令可以列出系统中当前连接的硬盘 设备和分区信息.新硬盘没有分区信息,则只显示硬盘大小信息. 1.关闭服务器加上新硬盘 2.启动 ...
- Linux 用户操作之用户管理 (用户增删改操作)
目录 添加用户 删除用户 修改用户 切换用户 配置用户密码 查看配置文件 cat /etc/pwsswd 添加用户 可选项 -c comment 指定一段注释性描述. -d 目录 指定用户主目录,如果 ...
- 【Vue】Vue框架常用知识点 Vue的模板语法、计算属性与侦听器、条件渲染、列表渲染、Class与Style绑定介绍与基本的用法
Vue框架常用知识点 文章目录 Vue框架常用知识点 知识点解释 第一个vue应用 模板语法 计算属性与侦听器 条件渲染.列表渲染.Class与Style绑定 知识点解释 vue框架知识体系 [1]基 ...
- 【Linux】cp命令的各种妙用
CP 功能: 复制文件或目录 说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件 ...
- .NET, NETCORE 怎么写 "超时"代码,解析"超时"代码原理!
干货:本人不会长篇大论.能贴上去的,就是干货,能用一两句话讲明白的,不会大讲概念,不会浪费大家宝贵的时间. 前言:我们发现,超时是个非常重要的概念,如果在通讯架构中,没有超时的设计,那么这个通讯架构就 ...