AGC 16 D - XOR Replace
附上attack(自为风月马前卒爷) 的题解
Problem Statement
There is a sequence of length N: a=(a1,a2,…,aN). Here, each ai is a non-negative integer.
Snuke can repeatedly perform the following operation:
Let the XOR of all the elements in a be x. Select an integer i (1≤i≤N) and replace ai with x.
Snuke's objective is to match a with another sequence b=(b1,b2,…,bN). Here, each bi is a non-negative integer.
Determine whether the objective is achievable, and find the minimum necessary number of operations if the answer is positive.
Solution
\\ \Rightarrow A' = A_i
\]
转化一下思路, 就可以把异或这个东西去掉了, 操作就变成了这样的形式
- \(x = A_1\oplus A_2\oplus \cdots \oplus A_n\)
- \(A_p'=x, x = A_p\)
就想到一个做法, 对于一个长度为 n 的轮换, 答案是 n 或者 n + 1
但是如何找这个轮换呢?
我想了2个多小时.
并没有想到用图论的做法做.
最后写了写
只有80分(数据水的吓人) .可能是哪里写错了吧.
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
/*
A' = A \oplus A_i \oplus A
A' = A_i
......
对于一个长度为 n 的轮换, 答案是 n 或者 n + 1
*/
const int N = 1e5 + 7;
int A[N], B[N];
int a[N], b[N];
int QuChong(int n) {
int cnt = 0;
for (int i = 1; i <= n; i += 1)
if (A[i] != B[i]) a[++cnt] = A[i], b[cnt] = B[i];
return cnt;
}
map<int, int> S1, S2;
int vis[N], siz[N];
int main () {
freopen("replace.in", "r", stdin);
freopen("replace.out", "w", stdout);
int n;
int yihuohe = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i += 1) scanf("%d", &A[i]);
for (int j = 1; j <= n; j += 1) scanf("%d", &B[j]);
for (int i = 1; i <= n; i += 1) yihuohe ^= A[i];
int cnt = QuChong(n);
for (int i = 1; i <= cnt; i += 1) S1[a[i]] = i, S2[b[i]] = i;
int numdiff = 0;
for (int i = 1; i <= cnt; i += 1) if (not S2.count(a[i])) numdiff += 1;
if (numdiff > 1) { printf("-1\n"); return 0; }
int res = cnt;
int temp = yihuohe, tmp;
for (int i = 1; i <= cnt; i += 1) {
if (a[i] == b[i]) continue;
while (S2[temp] and not vis[S2[temp]]) {
tmp = temp, temp = a[S2[temp]];
// printf("get: %d %d\n", tmp, S2[tmp]);
a[S2[tmp]] = tmp, vis[S2[tmp]] = true;
}
// for (int i = 1; i <= cnt; i += 1) printf("%d ", a[i]); puts("");
if (a[i] == b[i]) continue;
tmp = temp, res += 1, temp = a[i], a[i] = tmp;
}
printf("%d\n", res);
return 0;
}
AGC 16 D - XOR Replace的更多相关文章
- AGC016D - XOR Replace 置换/轮换
目录 题目链接 题解 代码 题目链接 AGC016D - XOR Replace 题解 可以发现一次操作相当于一次置换 对于每个a上的位置映射到b对应 可以找到置换群中的 所有轮换 一个k个元素的轮换 ...
- AtcoderGrandContest 016 D.XOR Replace
$ >AtcoderGrandContest \space 016 D.XOR\space Replace<$ 题目大意 : 有两个长度为 \(n\) 的数组 \(A, B\) ,每次操作 ...
- 16. orcle中replace的用法及例子
replace 函数用法如下: replace('将要更改的字符串','被替换掉的字符串','替换字符串'); 例子: select replace ('1,2,3',',',';') from d ...
- agc016D - XOR Replace(图论 智商)
题意 题目链接 给出两个长度为\(n\)的数组\(a, b\) 每次可以将\(a\)中的某个数替换为所有数\(xor\)之和. 若\(a\)数组可以转换为\(b\)数组,输出最少操作次数 否则输出\( ...
- 【做题】agc016d - XOR Replace——序列置换&环
原文链接 https://www.cnblogs.com/cly-none/p/9813163.html 题意:给出初始序列\(a\)和目标序列\(b\),都有\(n\)个元素.每次操作可以把\(a\ ...
- 【agc016D】XOR Replace
Portal --> agc016D Description 一个序列,一次操作将某个位置变成整个序列的异或和,现在给定一个目标序列,问最少几步可以得到目标序列 Solution 翀 ...
- 【题解】Atcoder AGC#16 E-Poor Turkeys
%拜!颜神怒A此题,像我这样的渣渣只能看看题解度日╭(╯^╰)╮在这里把两种做法都记录一下吧~ 题解做法:可以考虑单独的一只鸡 u 能否存活.首先我们将 u 加入到集合S.然后我们按照时间倒序往回推, ...
- Agc016_D XOR Replace
传送门 题目大意 给定两个长为$n$的序列$A,B$你可以进行若干组操作,每次操作选定一各位置$x$,令$A_x$等于$A$的异或和. 问能否通过一定操作使得$A$成为$B$,如果能,求最小操作书数. ...
- [agc016d]xor replace
题意: 题解: 棒棒的神仙题...这题只是D题???(myh:看题五分钟,讨论两小时) 首先这个异或和是假的,比如我现在有$a=(a_1,a_2,a_3,a_4)$,操作一下$a_2$,就变成了$a= ...
随机推荐
- arp 投毒实验
1.查看kali2.0和kali2.0.0的IP地址,如图1和图2,其中192.168.1.133作为攻击者,192.168.1.109作为PC访问FTP服务器192.168.1.234 图1 图2 ...
- javaWeb中,文件上传和下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - C 暴力 STL
What Kind of Friends Are You? Time Limit: 1 Second Memory Limit: 65536 KB Japari Park is a larg ...
- Qt ------ QTabWidget
下图: 1.长方形的 objectName 可写可不写,不写就作用于所有 QTabWidget:椭圆形的 QTabWidget#tabWidget 要么四个都要写,要么四个都不写 2.下图的 CSS ...
- 「Python实践」学习之路
一.列表内容对比 方式一: import operatorl1 = ['a','b','c']l2 = ['a','d','e']print(operator.lt(l1,l2))运行结果为True ...
- OpenCV---直方图反向投影
一:直方图反向投影的方法 二:二维直方图的表示 (一)直接显示 def hist2D_demo(image): hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV) hi ...
- Debian sudo自动补全
解决 debian sudo TAB 键不能自动补全命令的原因 一般情况,命令行输入 sudo apt-get ins 按 tab ,它后面会自动补全为 install 如果右面写了包的名的一部分,按 ...
- 2015/9/29 Python基础(20):类的授权
类的授权 1.包装包装在Python编程世界中时经常会被提到的一个术语.它是一个通用的名字,意思是对一个已存在的对象进行包装,不管它是数据类型,还是一段代码,可以是对一个已存在的对象,增加新的,删除不 ...
- JVM学习十:JVM之垃圾收集器及GC参数
接近两个月左右没有写博客,主要是因为小孩过来后,回家比较忙,现在小孩端午送回家了,开始继续之前的JVM学习之路,前面学习了GC的算法和种类,那么本章则是基于算法来产生实际的用途,即垃圾收集器. 一.堆 ...
- 阿里云 配置FTP 无法连接问题,2017年7月后
被阿里云坑飞,2017年7月上线的安全组,我压根不知道 新服务器 配置防火墙后怎么也不起作用,也暂时关不了防火墙 iptables stop #无效 搞了半天 才知道TM 不需要在linx服务器上自 ...