Codeforces Round #658 (Div. 2)
A.Common Subsequence
题意
给你两组数,问你有没有相同 的书,有的话,输出最短的那组(大家都知道,1是最小的)
AC
#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
int a[N], x, n, m, flag, t;
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t; while (t--) {
cin >> n >> m;
for (int i = 0; i < n; ++i)cin >> x, a[x]++;
flag = 0;
for (int i = 0; i < m; ++i) {
cin >> x;
if (a[x])
flag = x;
}
if (flag) {
cout << "YES" << endl;
cout << 1 << " " << flag << endl;
}
else cout << "NO" << endl;
}
}
B.Sequential Nim
题意:
两个人玩区石子游戏,有n堆,第i堆有a[i]个,每个人只能按堆的顺序拿,就是前面这堆没有拿完,不能拿下一堆。谁先不能拿就输了。
思路:
谁先遇到大于1的石子堆,谁就一定不会输,因为大于1的石子堆,我可以选择全拿完和留一个,这两种状态结果是互斥的,必定会有一个状态的必胜态,所以只要判断到大于1的数前面的1的数量就行,若是全1的情况,那就轮流拿,单独判断一下就行。
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, t, x, flag, cnt;
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t; while (t--) {
cin >> n; cnt = 0; flag = 0;
while (n--) {
cin >> x;
if (x == 1 && flag == 0)
cnt++;
if (x > 1)
flag = 1;
}
if ((cnt % 2 == 1 && flag) || (cnt % 2 == 0 && flag == 0))
cout << "Second" << endl;
else
cout << "First" << endl;
}
}
C1Prefix Flip (Easy Version)
题意;
给你两个长度相同的01字符串a,b,有一种操作,我们可以把一个字符串长度为x的前缀拿出来,把0,1互换,(就像是异或一下)然后再把这个前缀翻转(掉个头)放回到原字符串中,问我们通过几次这种操作把a转换为b。操作数小于2*n;
思路:
因为我们每次拿的都是前缀,那么也就是说,后面的不会动了,那么我们可以从后往前来,遇到不同的,就判断a开头位置和b当前位置(因为不同就要进行”异或“然后翻转,会把第一个数翻转到当前位置上,所以判断a第一个位置和b当前位置)
如果第一位置和当前位置相同,要把第一位置单独转一下,(因为相同,异或再转过来就不同了)记录一下每次翻转的位置就是答案。
#include<bits/stdc++.h>
using namespace std;
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
string a, b; int t; cin >> t;
while (t--) {
int n; cin >> n;
cin >> a >> b; int ans = 0;
for (int i = 0; i < n; ++i)if (a[i] != b[i])ans++;
cout << 3 * ans << " ";
for (int i = 0; i < n; ++i)if (a[i] != b[i])cout << i + 1 << " 1 " << i + 1 << " ";
cout << endl;
}
}
C2题待补
D. Unmerge(01背包问题,1800)
题意:
对于两个数组,定义merge运算:每次把两个数组的首部的较小的那个拿出,并放到一个新的数组中。现在给定一个长度为n*2的排列,问你这个排列是否可能由两个长度为n的数组merge得到?可能则打印yes,否则no。
思路:
因为每次拿头部最小的放进新数组,所以一旦我们碰到一个数值a[i],紧跟a[i]后面的并且小于a[i]的全都应该和a[i]是同一组的,那么我们对于每次出现的这种,我们记录它们的长度。如果可能由两个长度为n的组成,那么我们可以找到一些长度,他们加起来等于n,也就是01背包问题了。
#include<bits/stdc++.h>
using namespace std;
const int N = 4e3 + 10;
int dp[N], d[N], a[N];
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t, n; cin >> t; while (t--) {
cin >> n;
memset(dp, 0, sizeof dp);
for (int i = 1; i <= 2 * n; ++i)cin >> a[i];
int mx = a[1], cnt = 1, len = 0;
for (int i = 2; i <= 2 * n; ++i) {
if (a[i] > mx) {
mx = a[i];
d[++len] = cnt;
cnt = 1;
}
else
cnt++;
}
d[++len] = cnt;
for (int i = 1; i <= len; i++)
for (int j = n; j >= d[i]; j--)
dp[j] = max(dp[j], dp[j - d[i]] + d[i]);
if (dp[n] == n) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
E题待补
Codeforces Round #658 (Div. 2)的更多相关文章
- Codeforces Round #658 (Div. 2) D. Unmerge(dp)
题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...
- Codeforces Round #658 (Div. 2)【ABC2】
做完前四题还有一个半小时... 比赛链接:https://codeforces.com/contest/1382 A. Common Subsequence 题意 给出两个数组,找出二者最短的公共子序 ...
- Codeforces Round #658 (Div. 2) D. Unmerge (思维,01背包)
题意:有两个数组\(a\)和\(b\),每次比较它们最左端的元素,取小的加入新的数组\(c\),若\(a\)或\(b\)其中一个为空,则将另一个全部加入\(c\),现在给你一个长度为\(2n\)的数组 ...
- Codeforces Round #658 (Div. 2) C2. Prefix Flip (Hard Version) (构造)
题意:给你两个长度为\(n\)的01串\(s\)和\(t\),可以选择\(s\)的前几位,取反然后反转,保证\(s\)总能通过不超过\(2n\)的操作得到\(t\),输出变换总数,和每次变换的位置. ...
- Codeforces Round #658 (Div. 2) C1. Prefix Flip (Easy Version) (构造)
题意:给你两个长度为\(n\)的01串\(s\)和\(t\),可以选择\(s\)的前几位,取反然后反转,保证\(s\)总能通过不超过\(3n\)的操作得到\(t\),输出变换总数,和每次变换的位置. ...
- 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 ...
随机推荐
- python的动态绑定属性和方法
目录 创建类 动态绑定属性 动态绑定方法 创建类 首先我们创建一个类和它的对象 class Student: def __init__(self, name, age): self.name = na ...
- R数据分析:集成学习方法之随机生存森林的原理和做法,实例解析
很久很久以前给大家写过决策树,非常简单明了的算法.今天给大家写随机(生存)森林,随机森林是集成了很多个决策数的集成模型.像随机森林这样将很多个基本学习器集合起来形成一个更加强大的学习器的这么一种集成思 ...
- 【VMware vCenter】使用cmsso-util命令进行链接、删除、修改多个vCenter Server(VCSA)的SSO域。
VMware vCenter Server 支持新安装的时候选择将vCenter SSO域加入到另外一个现有的SSO域中,同时也支持使用cmsso-util命令将现有的两个或多个vCenter SSO ...
- 小心C#中的只读结构体成员
示例 我们先来看一段结构体的代码 (基于 VS2022 + .NET 8.0) public struct MyStruct(int number) { public int Number = num ...
- 配置postcss-pxtorem报:options has an unknown property 'plugins'
闲聊: 小颖最近在坐大屏相关的项目,要写适配,之前用的:postcss-px2rem.px2rem-loader,和朋友闲聊呢他说他们也在写大屏,不过他们用的 postcss-pxtorem,在写另外 ...
- 环形缓冲区 Ring Buffer 的实现
环形缓冲区(Circular Buffer 或 Ring Buffer)是一种数据结构,它在逻辑上形成一个闭环.这种结构非常适用于需要固定大小的缓冲区的情况,如音频处理.网络通信.实时数据传输等.环形 ...
- Codeforces Round 905 (Div. 3)
Codeforces Round 905 (Div. 3) A. Morning 题意:操作:显示,向前走都为一次操作:目标:显示这四个数 思路:0->10,然后依次作差就行 #include ...
- [HDU4117] GRE
Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...
- 如何判断lib和dll是32位还是64位?答案是使用微软的dumpbin工具,后面讲了如何使用gcc生成lib和dll
为什么我会考虑这个问题呢?因为我在使用java去调用一个c的lib库的时候,弹出以下警告: D:\work\ideaworkpaces\jdk21Test001\src\main\java\lib\h ...
- teleport 服务端配置文件说明
teleport 服务端配置文件说明 teleport配置文件位于/usr/local/teleport/data/etc目录下.服务器端包含两个配置文件: core.ini 和 web.ini,其中 ...