比赛链接:https://codeforces.com/contest/1546

A. AquaMoon and Two Arrays

题意

给出两个大小为 \(n\) 的数组 \(a, b\) ,每次可以选择 \(a\) 中的两个元素分别加一减一,计算将 \(a\) 变为 \(b\) 的操作次数和步骤。

题解

数据范围较小,直接模拟即可。

若数据范围较大,可用栈或队列分别存储 \(a_i \lt b_i\) 和 \(a_i \gt b_i\) 的数,这样每次查找的时间复杂度就降到了 \(O_{(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> a(n), b(n);
int suma = 0, sumb = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
suma += a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
sumb += b[i];
}
if (suma != sumb) {
cout << -1 << "\n";
continue;
}
vector<pair<int, int>> op;
for (int i = 0; i < n; i++) {
while (a[i] < b[i]) {
for (int j = i + 1; j < n; j++) {
if (a[j] > b[j]) {
--a[j], ++a[i];
op.emplace_back(j, i);
break;
}
}
}
while (a[i] > b[i]) {
for (int j = i + 1; j < n; j++) {
if (a[j] < b[j]) {
--a[i], ++a[j];
op.emplace_back(i, j);
break;
}
}
}
}
cout << op.size() << "\n";
for (auto [x, y] : op) {
cout << x + 1 << ' ' << y + 1 << "\n";
}
}
return 0;
}

B. AquaMoon and Stolen String

题意

有 \(n\) 个字符串,将 \(n - 1\) 个字符串中同一位上的字符相互交换,给出原来的 \(n\) 个字符串和操作后的 \(n - 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, m;
cin >> n >> m;
string ans(m, 0);
for (int i = 0; i < 2 * n - 1; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
ans[j] ^= s[j];
}
}
cout << ans << "\n";
}
return 0;
}

C. AquaMoon and Strange Sort

题意

给出 \(n\) 个数,初始时均朝右(朝向不影响数值大小),每次操作可以选取相邻的两个数交换位置并反转朝向,判断能否将 \(n\) 个数排为升序后仍都朝右。

题解

若一个数操作后仍朝右,那么它移动的距离一定是 2​ 的倍数,即位置的奇偶性不变,判断排序前后同一值的奇偶位置个数是否相同即可。

代码

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> pos_before(N, vector<int> (2));
for (int i = 0; i < n; i++) {
++pos_before[a[i]][i & 1];
}
sort(a.begin(), a.end());
vector<vector<int>> pos_after(N, vector<int> (2));
for (int i = 0; i < n; i++) {
++pos_after[a[i]][i & 1];
}
cout << (pos_before == pos_after ? "Yes" : "No") << "\n";
}
return 0;
}

D. AquaMoon and Chess

题意

给出一个 01 串,每个 1 只能隔着一个 1 与 0 交换位置,计算 01 串最终可能状态的个数。

题解

将相邻的两个 1 看作一个整体,设 0 的个数为 \(n\) ,无交集的 11 个数为 \(m\) ,答案即 \(C_{n + m}^{m}\) 。

在奇数长度的连续 1 串中,有一个 1 因为无法移动,在所有可能状态中的位置都是固定的。

代码

#include <bits/stdc++.h>
using namespace std; constexpr int N = 1e6 + 100;
constexpr int MOD = 998244353; int fac[N], inv[N]; int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
} int C(int n, int m){
if(m < 0 or m > n) return 0;
return 1LL * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
} void Init(){
fac[0] = 1;
for (int i = 1; i < N; i++) fac[i] = 1LL * fac[i - 1] * i % MOD;
inv[N - 1] = binpow(fac[N - 1], MOD - 2);
for (int i = N - 2; i >= 0; i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD;
} int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Init();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
int one = 0;
for (int i = 0; i + 1 < n; i++) {
if (s[i] == '1' and s[i + 1] == '1') {
++one, ++i;
}
}
int zero = count(s.begin(), s.end(), '0');
cout << C(one + zero, one) << "\n";
}
return 0;
}

参考

https://codeforces.com/blog/entry/92739

后记

蛮有思维难度的一场比赛,学到很多 ( ̄▽ ̄) /

Codeforces Round #732 (Div. 2)【ABCD】的更多相关文章

  1. Codeforces Round #682 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...

  2. Codeforces Round #678 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...

  3. Codeforces Round #676 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...

  4. Codeforces Round #675 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...

  5. Codeforces Round #668 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...

  6. Codeforces Round #677 (Div. 3)【ABCDE】

    比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...

  7. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  8. Codeforces Round #684 (Div. 2)【ABC1C2】

    比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #includ ...

  9. Codeforces Round #658 (Div. 2)【ABC2】

    做完前四题还有一个半小时... 比赛链接:https://codeforces.com/contest/1382 A. Common Subsequence 题意 给出两个数组,找出二者最短的公共子序 ...

随机推荐

  1. 安装了Python2.X和Python3.X后Python2.X IDLE打不开解决办法总结

    安装了Python2.X和Python3.X后Python2.X IDLE打不开,两个版本都卸载后重装仍然打不开,在网上找了几种办法,希望对大家能有所帮助 1.首先查看环境变量是否配置正确 配置方法网 ...

  2. EasyUI_使用datagrid分页 (Day_28)

    本次分页涉及技术点 SSM+PageHelper+DatagrId 先来看下效果: 这是无条件分页,下一篇博客我们将讲有条件分页. 无论你是使用js加载table 还是直接使用标签. 使用datagr ...

  3. rocketmq常见问题及使用 新手篇

    一 部署阶段 1.启动命令 nameServer启动:nohup sh bin/mqnamesrv  -n ip地址:9876 & broker启动:nohup sh bin/mqbroker ...

  4. Spring Cloud Alibaba系列之分布式服务组件Dubbo

    本博客的例子代码可以在github找到下载链接:代码下载 SpringBoot.SpringCloud Alibaba系列博客专栏:链接 1.分布式理论 1.1.分布式基本定义 <分布式系统原理 ...

  5. GPU特征处理技术

    GPU特征处理技术 GPU和CPU有何不同? 现代片上系统(SoC)通常集成中央处理器(CPU)和图形处理器(GPU).设计不同,这可能更取决于处理的数据集的类型. CPU经过优化,可以一次对几块数据 ...

  6. TensorFlow+TVM优化NMT神经机器翻译

    TensorFlow+TVM优化NMT神经机器翻译 背景 神经机器翻译(NMT)是一种自动化的端到端方法,具有克服传统基于短语的翻译系统中的弱点的潜力.本文为全球电子商务部署NMT服务. 目前,将Tr ...

  7. 使用NVIDIA GRID vPC支持视频会议和算力工具

    随着2020年的发展,远程工作解决方案已成为许多人的新常态.企业正在寻找行之有效的解决方案,如虚拟桌面基础设施(VDI),以使他们的团队能够在任何地方安全地工作.然而,最新的算力和视频会议应用程序需要 ...

  8. JVM--你常见的jvm 异常有哪些? 代码演示:StackOverflowError , utOfMemoryError: Java heap space , OutOfMemoryError: GC overhead limit exceeded, Direct buffer memory, Unable_to_create_new_native_Thread, Metaspace

    直接上代码: public class Test001 { public static void main(String[] args) { //java.lang.StackOverflowErro ...

  9. 【读书笔记】《C语言 从入门到精通》(第三版)笔记

    C语言,上学的时候都没学好,没想到现在却靠它吃饭.因为对C语言还是比较熟悉,所以买这本书是用来当"字典"用的.所以下面的笔记不会有很基础的内容. 1.书籍介绍 2.结构体 3.[C ...

  10. DarkGreenTrip博客搭建成功

    本博客(https://www.cnblogs.com/zhangshuhao1116)自2021年6月19日由 Shu-How Z  搭建成功,2018年搭建过hexo+next.Wordpress ...