AGC 16 D - XOR Replace
2024-09-19 06:02:17
附上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
\]
\\ \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;
}
最新文章
- 杂记之web篇
- Datatable根据多行排序
- self 和 super 关键字
- AppDelegate减负之常用三方封装 - 友盟分享 / 三方登录篇
- Python实战之Selenium自动化测试web刷新FW
- 微服务领域是不是要变天了?Spring Cloud Alibaba正式入驻Spring Cloud官方孵化器!
- 同时使用antd和css module
- css 设计规范
- R绘制3D散点图
- STL进阶--成员函数 vs 算法
- IDEA Maven项目 编译的问题
- NC 5的开发环境起不了客户端
- android Service oncreate 在UI线程 何时用service,何时用thread
- .Net的混淆防反编译工具ConfuserEx--2(转)
- 使用ExtJS做一个用户的增删改查
- Axios使用文档总结
- 通过bat设置系统环境变量
- input标签与label标签的“合作关系”
- [C++] 递归之全排列问题、半数集
- 读<;<;programming ruby>;>; 7.6节 flip-flop 理解
热门文章
- 【模考】2018.04.08 Connection
- flex的使用实例
- 【bzoj2500】幸福的道路
- Linux应用编程之串口操作20170901
- Myeclipse下配置SVN报错问题 svn: E175002: java.lang.RuntimeException: Could not generate DH keypair,缺少subclipse插件的javaHL
- C++ ------ 创建对象 new 和不 new 的区别
- nodejs express框架一个工程中同时使用ejs模版和jade模版
- Python/spss-多元回归建模-共线性诊断2(推荐AA)
- js 30分钟倒计时
- 随机切分csv训练集和测试集