AGC016D - XOR Replace 置换/轮换
目录
题目链接
题解
可以发现一次操作相当于一次置换
对于每个a上的位置映射到b对应
可以找到置换群中的 所有轮换
一个k个元素的轮换需要k+1步完成
那么答案就是边数+轮换数-1
-1的话发现当最一个数为缺少的数时不需吧最后一步换回来
代码
#include<map>
#include<cstdio>
#include<algorithm>
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9')c = gc;
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
void print(int x) {
if(x < 0) {
pc('-');
x = -x;
}
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 1000007;
int a[maxn],b[maxn],c[maxn],d[maxn];
int n;
std::map<int,int>f;
int fa[maxn];
int find(int x) {
if(fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
n = read();
for(int i = 1;i <= n;++ i) a[i] = read();
for(int i = 1;i <= n;++ i) b[i] = read();
int t = 0;
for(int i = 1;i <= n;++ i) t ^= a[i];
a[n + 1] = t;
t = 0;
for(int i = 1;i <= n;++ i) t ^= b[i];
b[++ n] = t;
for(int i = 1;i <= n;++ i) c[i] = a[i],d[i] = b[i];
std::sort(c + 1,c + n + 1);
std::sort(d + 1,d + n + 1);
for(int i = 1;i <= n;++ i) {
if(c[i] != d[i]) {
puts("-1");
return 0;
}
}
int tot = 0;
int ans = 0;
for(int i = 1;i <= n;++ i) {
if(a[i] != b[i] || i == n) {
if(i < n) ans ++;
if(!f[a[i]])f[a[i]] = ++tot;
if(!f[b[i]])f[b[i]] = ++tot;
}
}
if(!ans) {
pc('0');
return 0;
}
for(int i = 1;i <= tot;++ i) fa[i] = i;
for(int i = 1;i <= n;++ i) {
if(a[i] != b[i]) fa[find(f[a[i]])] = find(f[b[i]]);
}
for(int i = 1;i <= tot;++ i) if(fa[i] == i) ans ++;
print(ans - 1);
return 0;
}
AGC016D - XOR Replace 置换/轮换的更多相关文章
- 【做题】agc016d - XOR Replace——序列置换&环
原文链接 https://www.cnblogs.com/cly-none/p/9813163.html 题意:给出初始序列\(a\)和目标序列\(b\),都有\(n\)个元素.每次操作可以把\(a\ ...
- agc016D - XOR Replace(图论 智商)
题意 题目链接 给出两个长度为\(n\)的数组\(a, b\) 每次可以将\(a\)中的某个数替换为所有数\(xor\)之和. 若\(a\)数组可以转换为\(b\)数组,输出最少操作次数 否则输出\( ...
- [agc016d]xor replace
题意: 题解: 棒棒的神仙题...这题只是D题???(myh:看题五分钟,讨论两小时) 首先这个异或和是假的,比如我现在有$a=(a_1,a_2,a_3,a_4)$,操作一下$a_2$,就变成了$a= ...
- AGC 16 D - XOR Replace
AGC 16 D - XOR Replace 附上attack(自为风月马前卒爷) 的题解 Problem Statement There is a sequence of length N: a=( ...
- 【agc016D】XOR Replace
Portal --> agc016D Description 一个序列,一次操作将某个位置变成整个序列的异或和,现在给定一个目标序列,问最少几步可以得到目标序列 Solution 翀 ...
- AtcoderGrandContest 016 D.XOR Replace
$ >AtcoderGrandContest \space 016 D.XOR\space Replace<$ 题目大意 : 有两个长度为 \(n\) 的数组 \(A, B\) ,每次操作 ...
- Agc016_D XOR Replace
传送门 题目大意 给定两个长为$n$的序列$A,B$你可以进行若干组操作,每次操作选定一各位置$x$,令$A_x$等于$A$的异或和. 问能否通过一定操作使得$A$成为$B$,如果能,求最小操作书数. ...
- Atcoder D - XOR Replace(思维)
题目链接:http://agc016.contest.atcoder.jp/tasks/agc016_d 题解:稍微想一下就知道除了第一次的x是所有的异或值,之后的x都是原先被替换掉的a[i]所以要想 ...
- AGC016题解
呼我竟然真的去刷了016QwQ[本来以为就是个flag的233] 感觉AGC题目写起来都不是很麻烦但是确实动脑子qvq[比较适合训练我这种没脑子选手] 先扔个传送门:点我 A.Shrinking 题意 ...
随机推荐
- elasticsearch更改mapping(不停服务重建索引)
转载地址:http://donlianli.iteye.com/blog/1924721?utm_source=tuicool&utm_medium=referral Elasticsearc ...
- docker的基础命令
详细命令参考http://www.runoob.com/docker/docker-command-manual.html
- 搭建vsf
参考: 1.https://blog.csdn.net/liuzhenwen/article/details/7026263 我是全部替换了/lib/为/lib64/ 2.https://blog.c ...
- 3.24网络攻防选拔题部分write up
20175221 3.24网络攻防选拔题部分write up Q1:百度一下,你就知道 解压题目得到一个文件夹和一个网址 打开文件夹,有三张图片 用winhex和stegsolve查看三张图片,没有 ...
- hive笔记
cast cast(number as string), 可以将整数转成字符串 lpad rpad lpad(target, 10, '0') 表示在target字符串前面补0,构成一个长度为 ...
- 第七节:利用CancellationTokenSource实现任务取消和利用CancellationToken类检测取消异常。
一. 传统的线程取消 所谓的线程取消,就是线程正在执行的过程中取消线程任务. 传统的线程取消,是通过一个变量来控制,但是这种方式,在release模式下,被优化从cpu高速缓存中读取,而不是从内存中读 ...
- 第九节:深究并行编程Parallel类中的三大方法 (For、ForEach、Invoke)和几大编程模型(SPM、APM、EAP、TAP)
一. 并行编程 1. 区分串行编程和串行编程 ①. 串行编程:所谓的串行编程就是单线程的作用下,按顺序执行.(典型代表for循环 下面例子从1-100按顺序执行) ②. 并行编程:充分利用多核cpu的 ...
- DTO/DO等POJO对象的使用场景和 orika-mapper 框架的使用
对于项目而言, 我们一般会有DAO->Service->Controller分层设计, 这些层次体现了每层的作用, 而层次之间的数据传递对象设计很少被提及, 下面是一个相对完整的数据转换过 ...
- JAVA进阶10
间歇性混吃等死,持续性踌躇满志系列-------------第10天 1.Random package cn.intcast.day08.demo01; import java.util.Random ...
- 编程入门视频【 Python、PHP、ThinkPHP、Laravel、Mysql、微信小程序】
免费分享 Python.PHP.ThinkPHP.Laravel.Mysql.微信小程序等学习视频 点击进入搜刮 免费分享 Python.PHP.ThinkPHP.Laravel.Mysql.微信小程 ...