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= ...
随机推荐
- 【bzoj4810】【ynoi2018】由乃的玉米田
4810: [Ynoi2017]由乃的玉米田 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 1090 Solved: 524[Submit][Sta ...
- OpenCV C++如何使RGB图像变为灰度图像
http://m.blog.csdn.net/blog/u014395105/41308979 最近在研究如何用C++来处理图像,而不使用封装好的OpenCV代码,这样能够更好的了解OpenCV的内部 ...
- Measure the size of a PostgreSQL table row
Q: I have a PostgreSQL table. select * is very slow whereas select id is nice and quick. I think i ...
- 【OpenCV】SIFT原理与源码分析:关键点搜索与定位
<SIFT原理与源码分析>系列文章索引:http://www.cnblogs.com/tianyalu/p/5467813.html 由前一步<DoG尺度空间构造>,我们得到了 ...
- poj1006 生理周期
生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138947 Accepted: 44597 Descripti ...
- golang设置代理
http://note.youdao.com/noteshare?id=a8df0ec2d623f282a782dbe937bdae9f
- php数组定义
$arr = array( ,), //是’0' ,不是[‘0’] ,), ,), ,), ); $arr = [ '0' => array(2,3), //是’0' ,不是[‘0’] '1' ...
- [LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes
引言 Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧. 例题 1 Given a 2D board cont ...
- echart图跟随屏幕自适应变化
var myChart = echarts.init(document.getElementById('main'),'macarons');// var option = { //...一些配置 / ...
- NOIP模拟赛11
T1 [HAOI2016]放棋子 https://daniu.luogu.org/problem/show?pid=3182 障碍交换行不影响 所以第i列有障碍的行换到第i行 然后错排公式 本校自测要 ...