AtCoder Grand Contest 016 D - XOR Replace

63 篇文章 0 订阅
15 篇文章 0 订阅

Time limit : 2sec / Memory limit : 256MB

Score : 1000 points

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.

Constraints

2≤N≤105
ai and bi are integers.
0≤ai,bi<230

Input

Input is given from Standard Input in the following format:

N
a1 a2 … aN
b1 b2 … bN

Output

If the objective is achievable, print the minimum necessary number of operations. Otherwise, print -1 instead.

Sample Input 1

3
0 1 2
3 1 0

Sample Output 1

2
At first, the XOR of all the elements of a is 3. If we replace a1 with 3, a becomes (3,1,2).

Now, the XOR of all the elements of a is 0. If we replace a3 with 0, a becomes (3,1,0), which matches b.

Sample Input 2

Copy
3
0 1 2
0 1 2

Sample Output 2

0

Sample Input 3

2
1 1
0 0

Sample Output 3

-1

Sample Input 4

4
0 1 2 3
1 0 3 2

Sample Output 4

5

Gist

  • 给你两个序列 a 和序列 b ,长度各为 N

  • 要求最少的变换操作数,使得将序列 a 变为序列 b

  • 操作:令 x=序列 a 每个元素的异或和,并用 x 代替一个 ai

  • 数据范围: 2N105 0ai,bi<230

Solution

  • 要会做这题,就必须发现其操作的本质。

  • 发现除了第一次求异或和产生的数是新数外,之后的数都是旧的(替换的)。

  • 因为替换后的异或和就是被替换的数本身(异或的性质)。

  • 那么为了方便处理,就将第一次的新数放到那 N 个数的末尾,

  • 操作就相当于将最后一个数与前面 N 个数中的某个数交换。

  • 不合法的情况就是将两列数排序后不完全相同(无论如何都不能换到相同)。

  • 之后某个位置上 a 要换成 b ,就 a b 连一条边,

  • 每个连通块就需要 Size+1 次交换操作(除非包含第 N+1 个数,可以少一次,即答案减一)。

  • 用一个并查集维护即可,注意存数判断时要用 Hash 表。

  • 时间复杂度 O(N log N)

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
const int N=1e5+5,M=N*100+7;
int tot,ans;
int a[N],b[N],a1[N],b1[N];
int f[N],bel[M],h[M];
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
inline int get(int x)
{
    return f[x]==x?x:f[x]=get(f[x]);
}
inline int hash(int x)
{
    int y=x%M;
    while(h[y]>=0 && h[y]!=x) y=(y+1)%M;
    return y;
}
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)
    {
        a[i]=a1[i]=read();
        a[n+1]^=a[i];
    }
    a1[n+1]=a[n+1];
    for(int i=1;i<=n;i++)
    {
        b[i]=b1[i]=read();
        b[n+1]^=b[i];
    }
    b1[n+1]=b[n+1],n++;
    sort(a1+1,a1+1+n);
    sort(b1+1,b1+1+n);
    for(int i=1;i<=n;i++)
        if(a1[i]^b1[i]) return 0&printf("-1");
    memset(h,-1,sizeof(h));
    for(int i=1;i<=n;i++)
        if(i==n || a[i]^b[i])
        {
            if(i<n) ans++;
            int k=hash(a[i]);
            if(h[k]<0) h[k]=a[i],bel[k]=++tot;
            k=hash(b[i]);
            if(h[k]<0) h[k]=b[i],bel[k]=++tot;
        }
    for(int i=1;i<=tot;i++) f[i]=i;
    for(int i=1;i<=n;i++)
        if(a[i]^b[i]) f[get(bel[hash(a[i])])]=get(bel[hash(b[i])]);
    for(int i=1;i<=tot;i++)
        if(f[i]==i) ans++;
    printf("%d",ans-1);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AtCoder Practice Contest #B - インタラクティブ練習 (Interactive Sorting) 是一道比较有趣的题目。它是一道交互式的排序题目,需要你与一个神秘程序进行交互,以便将一串无序的数字序列排序。 具体来说,这个神秘程序会给你一个长度为 $N$ 的数字序列,然后你需要通过询问它两个数字的大小关系,来逐步确定这个序列的排序顺序。每次询问之后,神秘程序都会告诉你两个数字的大小关系,比如第一个数字比第二个数字小,或者第二个数字比第一个数字小。你需要根据这个信息,来调整这个数字序列的顺序,然后再向神秘程序询问下一对数字的大小关系,以此类推,直到这个数字序列被完全排序为止。 在这个过程中,你需要注意以下几点: 1. 你最多只能向神秘程序询问 $Q$ 次。如果超过了这个次数,那么你的程序会被判定为错误。 2. 在每次询问之后,你需要及时更新数字序列的顺序。具体来说,如果神秘程序告诉你第 $i$ 个数字比第 $j$ 个数字小,那么你需要将这两个数字交换位置,以确保数字序列的顺序是正确的。如果你没有及时更新数字序列的顺序,那么你的程序也会被判定为错误。 3. 在询问的过程中,你需要注意避免重复询问。具体来说,如果你已经询问过第 $i$ 个数字和第 $j$ 个数字的大小关系了,那么你就不需要再次询问第 $j$ 个数字和第 $i$ 个数字的大小关系,因为它们的大小关系已经被确定了。 4. 在排序完成之后,你需要将排序结果按照从小到大的顺序输出。如果你输出的结果不正确,那么你的程序也会被判定为错误。 总的来说,这道题目需要你熟练掌握交互式程序设计的技巧,以及排序算法的实现方法。如果你能够熟练掌握这些技巧,那么就可以顺利地完成这道非传统题了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值