Codeforces Round #658 (Div. 2)【ABC2】
做完前四题还有一个半小时...
比赛链接:https://codeforces.com/contest/1382
A. Common Subsequence
题意
给出两个数组,找出二者最短的公共子序列。
题解
最短即长为一。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n, m; cin >> n >> m;
set<int> a, b;
for (int i = 0; i < n; ++i) {
int x; cin >> x;
a.insert(x);
}
for (int i = 0; i < m; ++i) {
int x; cin >> x;
b.insert(x);
}
for (auto i : a) {
if (b.count(i)) {
cout << "YES" << "\n";
cout << 1 << ' ' << i << "\n";
return;
}
}
cout << "NO" << "\n";
} int main() {
int t; cin >> t;
while (t--) solve();
}
B. Sequential Nim
题意
给出 $n$ 堆石子的数量,两人轮流从最左端的非空堆中取任意数量的石子,无法再取者判负,判断游戏的胜者。
题解
第一次取到数量大于 $1$ 的石子堆的人获胜。
证明
将石子堆分为两类:连续的数量 $>1$ 的和单独的数量 $=1$ 的,第一次取到数量大于 $1$ 的石子堆的人可以通过取得剩一个或全部取完来迫使对方进入自己的节奏。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
int get_non_one = -1;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
if (x > 1 and get_non_one == -1) get_non_one = i;
}
if (get_non_one == -1) {
cout << (n & 1 ? "First" : "Second") << "\n";
} else {
cout << (get_non_one & 1 ? "First" : "Second") << "\n";
}
} int main() {
int t; cin >> t;
while (t--) solve();
}
C2. Prefix Flip (Hard Version)
题意
每次可以选取二进制串的前 $p$ 位全部取反然后反转,输出将二进制串 $a$ 变为 $b$ 的任一方案。
题解
先从前向后将 $a$ 的前缀合并为单一字符,然后从后向前操作与 $b$ 的不同位。
代码
#include <bits/stdc++.h>
using namespace std; void solve() {
int n; cin >> n;
string a, b; cin >> a >> b;
vector<int> p;
for (int i = 1; i < n; ++i) {
if (a[i] != a[i - 1]) {
p.push_back(i);
}
}
char now = a.back();
for (int i = n - 1; i >= 0; --i) {
if (now != b[i]) {
p.push_back(i + 1);
now = (now == '0' ? '1' : '0');
}
}
cout << p.size() << ' ';
for (int i : p) cout << i << ' ';
cout << '\n';
} int main() {
int t; cin >> t;
while (t--) solve();
}
Codeforces Round #658 (Div. 2)【ABC2】的更多相关文章
- Codeforces Round #677 (Div. 3)【ABCDE】
比赛链接:https://codeforces.com/contest/1433 A. Boring Apartments 题解 模拟即可. 代码 #include <bits/stdc++.h ...
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
- Codeforces Round #684 (Div. 2)【ABC1C2】
比赛链接:https://codeforces.com/contest/1440 A. Buy the String 题解 枚举字符串中 \(0\) 或 \(1\) 的个数即可. 代码 #includ ...
- Codeforces Round #682 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1438 A. Specific Tastes of Andre 题意 构造一个任意连续子数组元素之和为子数组长度倍数的数组. ...
- Codeforces Round #678 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1436 A. Reorder 题解 模拟一下这个二重循环发现每个位置数最终都只加了一次. 代码 #include <bi ...
- Codeforces Round #676 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1421 A. XORwice 题意 给出两个正整数 \(a.b\),计算 \((a \oplus x) + (b \oplus ...
- Codeforces Round #675 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...
- Codeforces Round #668 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1405 A. Permutation Forgery 题意 给出一个大小为 $n$ 的排列 $p$,定义 \begin{equ ...
- Codeforces Round #732 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1546 A. AquaMoon and Two Arrays 题意 给出两个大小为 \(n\) 的数组 \(a, b\) ,每 ...
随机推荐
- Spring Boot GraphQL 实战 03_分页、全局异常处理和异步加载
hello,大家好,我是小黑,又和大家见面啦~ 今天我们来继续学习 Spring Boot GraphQL 实战,我们使用的框架是 https://github.com/graphql-java-ki ...
- 改进你的c#代码的5个技巧(三)
本文完全独立于前两篇文章.如果你喜欢它们,我希望你也会喜欢这个.在上一篇文章中,我展示了哪种方法更快,并比较了代码的执行速度.在本文中,我将展示不同代码片段的内存消耗情况.为了显示内存映射和分配图,我 ...
- 设置.ignore后不生效解决方案
/logs/*.lock /logs/*.log/reports/API_TEST_V*/.idea/ 设置.ignore后不生效解决方案: 在terminal中输入已下几行代码: git rm -r ...
- ABAP program lines are wider than the internal table
错误详细描述: An exception occurred that is explained in detail below.The exception, which is assigned to ...
- 1.2V升3.3V芯片,大电流,应用MCU供电,3.3V稳压源
MCU供电一般是2.5V-5V之间等等都有,1.2V需要升到3.3V的升压芯片来稳压输出3.3V给MCU供电. 同时1.2V的输入电压低,说明供电端的能量也是属于低能量的,对于芯片自身供货是也要求高. ...
- Linux Centos7之由Python2升级到Python3教程
1.先查看当前系统Python版本,默认都是Python2.7,命令如下: [root@localhost gau]# python -V Python 2.7.5 2.安装Python3,安装方法很 ...
- uni-app开发经验分享二: uni-app生命周期记录
应用生命周期(仅可在App.vue中监听) 页面生命周期(在页面中添加) 当页面中需要用到下拉刷新功能时,打开pages.json,在"globalStyle"里设置"e ...
- 最佳的思维导图生成工具——markmap 使用教程
前言 相信很多程序员朋友都有在用 Markdown 吧,我是大三找实习工作的时候接触到的,简历就是用 Markdown 写的. Markdown 的好处是专注码字的同时还能兼顾排版,不用像 word ...
- Android事件分发机制五:面试官你坐啊
前言 很高兴遇见你~ 事件分发系列文章已经到最后一篇了,先来回顾一下前面四篇,也当个目录: Android事件分发机制一:事件是如何到达activity的? : 从window机制出发分析了事件分发的 ...
- (08)-Python3之--类和对象
1.定义 类:类是抽象的,一类事物的共性的体现. 有共性的属性和行为. 对象:具体化,实例化.有具体的属性值,有具体做的行为. 一个类 对应N多个对象. 类包含属性以及方法. class 类名: 属 ...