首页 > 其他分享 >CF1789 Codeforces Round 853 (Div. 2) D. Serval and Shift-Shift-Shift

CF1789 Codeforces Round 853 (Div. 2) D. Serval and Shift-Shift-Shift

时间:2023-03-05 21:14:11浏览次数:63  
标签:std 853 int Shift CF1789 cin -- include

https://codeforces.com/contest/1789/problem/D

给定两个 n 位二进制数 a,b,你可以每次使 \(a = a \oplus a >> k\) 或 \(a = a \oplus a << k\),你需要用不超过 n 次操作使得 \(a = b\),若无法达成输出 -1

显然 a 和 b 中仅有一个是 0 就无法达成

我们可以通过 a 的 1 来完成这个任务,由于不能超过 n 次操作,考虑每次操作都能固定一个位置,我们要保证每次操作不会影响我们已经确定好的数

首先尝试对齐 a 和 b 的最左端的 1,然后用这个 1 去更新它的右侧

然后考虑这个 1 的左端,我们可以用当前 a 中最小的 1 去更新,这样它的右侧全是 0,就不会影响我们已经调整好的部分

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <bitset>

#define i64 long long

const int mod = 998244353, inf = 0x3f3f3f3f;

int main() {
	int T; std::cin >> T;
	while (T--) {
		int n; std::cin >> n;
		std::string s, t; std::cin >> s >> t;
		if (s == t) {
			printf("0\n"); continue;
		}
		std::vector<int> a(n + 1), b(n + 1);
		for (int i = 1; i <= n; i++) {
			a[i] = s[i - 1] - '0';
			b[i] = t[i - 1] - '0';
		}
		if (std::count(a.begin() + 1, a.end(), 1) == 0) {
			printf("-1\n"); continue;
		}
		if (std::count(b.begin() + 1, b.end(), 1) == 0) {
			printf("-1\n"); continue;
		}
		int x = std::find(a.begin() + 1, a.end(), 1) - a.begin();
		int y = std::find(b.begin() + 1, b.end(), 1) - b.begin();
		std::vector<int> ans;
		if (x > y) {
			int d = x - y;
			for (int i = 1; i + d <= n; i++) a[i] ^= a[i + d];
			ans.push_back(d); x = y;
		}
		for (int i = x + 1; i <= n; i++) {
			if (a[i] == b[i]) continue;
			int d = i - x;
			for (int j = n; j > d; j--) a[j] ^= a[j - d];
			ans.push_back(-d);
		}
		int z = n; while (!a[z]) z--;
		for (int i = x; i >= 1; i--) {
			if (a[i] == b[i]) continue;
			int d = z - i;
			for (int j = 1; j <= i; j++) a[j] ^= a[j + d];
			ans.push_back(d);
		}
		int m = ans.size(); printf("%d\n", m);
		for (int i = 0; i < m; i++) printf("%d ", ans[i]); puts("");
	}
	return 0;
}

标签:std,853,int,Shift,CF1789,cin,--,include
From: https://www.cnblogs.com/zrzring/p/CF1789D.html

相关文章

  • CF1789D Serval and Shift-Shift-Shift 题解
    题目链接题目分析首先,看到题目中的左移右移之后再异或,我们自然可以想到在移动的过程中字符串的一段前缀和后缀不会改变,考虑通过这个性质逐位还原。因为异或0不会改变......
  • Codeforces Round 853 (Div. 2)(C,D)
    CodeforcesRound853(Div.2)(C,D)CC题目大意就是给你\(n\)个数,我们可以按顺序得到接下来的\(m\)个序列,每一个操作是对前面一个序列的第\(p\)个数变成\(v\),保证每次变......
  • 论文阅读笔记(四):AS-MLP AN AXIAL SHIFTED MLP ARCHITECTUREFOR VISION
    1.摘要本文提出了一种轴向移位的MLP体系结构(AS-MLP),更关注局部特征的交互,通过特征图的通道轴移动,AS-MLP能够从不同的轴获取信息,这使得网络能够捕捉局部依赖(可以理解为cn......
  • 关于OpenShift(OKD)通过命令行的方式部署打包镜像 Demo
    写在前面会陆续的和小伙伴分享一些OpenShift的笔记博文内容为安装完OpenShift,利用OpenShiftCICD流程部署应用的一个Demo理解不足小伙伴帮忙指正傍晚时分,你坐......
  • 关于OpenShift(OKD)网络Service、Routes的一些认识
    写在前面博文内容为OpenShift网络相关组件Service、Routes的一些认识理解不足小伙伴帮忙指正傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生......
  • Codeforces Round #853 (Div. 2) 题解
    CodeforcesRound#853(Div.2)题解ABCDCodeforcesRound#853(Div.2)|萌新实况被吊打|ABCD题解E.ServalandMusicGame分两种情况讨论:\(\lfloor\frac{s_......
  • 练习记录-cf-div2-853-(A-C)
    从11月打的只能写两题现在尽量3题了就在这里慢慢记录下练习吧看到成长也是学习的动力之一(补题会继续写,但不一定补的来今日总结:还是只会写简单思维题的菜狗(今天居然是20......
  • 853~855JQuery,广告显示隐藏,抽奖演示实现
    广告显示隐藏<!DOCTYPEhtml><html><head><metacharset="UTF-8"><title>广告的自动显示与隐藏</title><style>#content{width:100%;height:5......
  • 教你如何游戏里面禁止ctrl+shift 切输入法
    最近玩刀剑封魔录,这种老游戏,游戏里面ctrl+shift切输入法会很卡.有时候又会误操作. autohotkey搞定.代码如下:GroupAdd,game,ahk_exeSC2_x64.exe;注......
  • AWS Lambda 查询 Redshift Serverless
    在应用程序中,经常在Lambda中调用redshiftdataapi去查询redshiftserverless的数据,以下描述具体实现过程:1:给Lambda创建一个执行Lambda的IAMRole,并具有访问redshift......