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

\[A' = A \oplus A_i \oplus A
\\ \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的更多相关文章

  1. AGC016D - XOR Replace 置换/轮换

    目录 题目链接 题解 代码 题目链接 AGC016D - XOR Replace 题解 可以发现一次操作相当于一次置换 对于每个a上的位置映射到b对应 可以找到置换群中的 所有轮换 一个k个元素的轮换 ...

  2. AtcoderGrandContest 016 D.XOR Replace

    $ >AtcoderGrandContest \space 016 D.XOR\space Replace<$ 题目大意 : 有两个长度为 \(n\) 的数组 \(A, B\) ,每次操作 ...

  3. 16. orcle中replace的用法及例子

    replace 函数用法如下: replace('将要更改的字符串','被替换掉的字符串','替换字符串'); 例子: select  replace ('1,2,3',',',';') from d ...

  4. agc016D - XOR Replace(图论 智商)

    题意 题目链接 给出两个长度为\(n\)的数组\(a, b\) 每次可以将\(a\)中的某个数替换为所有数\(xor\)之和. 若\(a\)数组可以转换为\(b\)数组,输出最少操作次数 否则输出\( ...

  5. 【做题】agc016d - XOR Replace——序列置换&环

    原文链接 https://www.cnblogs.com/cly-none/p/9813163.html 题意:给出初始序列\(a\)和目标序列\(b\),都有\(n\)个元素.每次操作可以把\(a\ ...

  6. 【agc016D】XOR Replace

    Portal --> agc016D Description ​ 一个序列,一次操作将某个位置变成整个序列的异或和,现在给定一个目标序列,问最少几步可以得到目标序列 ​ Solution ​ 翀 ...

  7. 【题解】Atcoder AGC#16 E-Poor Turkeys

    %拜!颜神怒A此题,像我这样的渣渣只能看看题解度日╭(╯^╰)╮在这里把两种做法都记录一下吧~ 题解做法:可以考虑单独的一只鸡 u 能否存活.首先我们将 u 加入到集合S.然后我们按照时间倒序往回推, ...

  8. Agc016_D XOR Replace

    传送门 题目大意 给定两个长为$n$的序列$A,B$你可以进行若干组操作,每次操作选定一各位置$x$,令$A_x$等于$A$的异或和. 问能否通过一定操作使得$A$成为$B$,如果能,求最小操作书数. ...

  9. [agc016d]xor replace

    题意: 题解: 棒棒的神仙题...这题只是D题???(myh:看题五分钟,讨论两小时) 首先这个异或和是假的,比如我现在有$a=(a_1,a_2,a_3,a_4)$,操作一下$a_2$,就变成了$a= ...

随机推荐

  1. Codeforces710

    [未完待续] A The only king stands on the standard chess board. You are given his position in format &quo ...

  2. 【BZOJ3156】防御准备(动态规划,斜率优化)

    [BZOJ3156]防御准备(动态规划,斜率优化) 题面 BZOJ 题解 从右往左好烦啊,直接\(reverse\)一下再看题. 设\(f[i]\)表示第\(i\)个位置强制建立检查站时,前面都满足条 ...

  3. HDU1402:A * B Problem Plus——题解

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 给出两个高精度正整数,求它们的积,最长的数长度不大于5e4. FFT裸题,将每个数位看做是多项式的系数即可 ...

  4. 洛谷 P1023 税收与补贴问题 (2000NOIP提高组)

    洛谷 P1023 税收与补贴问题 (2000NOIP提高组) 题意分析 一开始没理解题意.啰啰嗦嗦一大堆.看了别人的题解才明白啥意思. 对于样例来说,简而言之: 首先可以根据题目推算出来 28 130 ...

  5. kotlin Hello World 以及关键字

    hello world:(支持函数式编程,不需要放到 class 中) fun main(args: Array<String>) { println("Hello World& ...

  6. Kubernetes Deployment滚动升级

    我们k8s集群使用的是1.7.7版本的,该版本中官方已经推荐使用Deployment代替Replication Controller(rc)了,Deployment继承了rc的全部功能外,还可以查看升 ...

  7. linux 隐藏权限

    原文 ------通过chattr设置档案的隐藏权限------ [root@sdc ~]#chattr --helpUsage: chattr [-RV] [-+=AacDdijsSu] [-v v ...

  8. [DeeplearningAI笔记]序列模型2.9情感分类

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.9 Sentiment classification 情感分类 情感分类任务简单来说是看一段文本,然后分辨这个人是否喜欢 ...

  9. HTML入门(一)

    ---恢复内容开始--- HTML 一 .HTML介绍 1. 什么是HTML? 超文本标记语言: 超文本: 比普通文本更强大 标记语言: 使用一组标签对内容进行描述的一门语言,它不是编程语言! 2. ...

  10. IIC总线学习

    IIC总线 IIC协议简要说明: 1.2条双向串行线,一条数据线称为SDA,一条时钟线SCL,双向半双工 2.传输的设备之间只是简单的主从关系,主机可以作为主机发送也可以作为主机接收,任何时候只能由一 ...