div class="part">

Problem Statement

You are given a sequence of non-negative integers $A=(a_1,\ldots,a_N)$.

Let us perform the following operation on $A$ just once.

  • Choose a non-negative integer $x$. Then, for every $i=1, \ldots, N$, replace the value of $a_i$ with the bitwise XOR of $a_i$ and $x$.

Let $M$ be the maximum value in $A$ after the operation. Find the minimum possible value of $M$.

What is bitwise XOR?

The bitwise XOR of non-negative integers $A$ and $B$, $A \oplus B$, is defined as follows.

  • When $A \oplus B$ is written in binary, the $k$-th lowest bit ($k \geq 0$) is $1$ if exactly one of the $k$-th lowest bits of $A$ and $B$ is $1$, and $0$ otherwise.

For instance, $3 \oplus 5 = 6$ (in binary: $011 \oplus 101 = 110$).

Constraints

  • $1 \leq N \leq 1.5 \times 10^5$
  • $0 \leq a_i \lt 2^{30}$
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$
$a_1$ $\ldots$ $a_N$

Output

Print the answer.


Sample Input 1

3
12 18 11

Sample Output 1

16

If we do the operation with $x=2$, the sequence becomes $(12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9)$, where the maximum value $M$ is $16$.

We cannot make $M$ smaller than $16$, so this value is the answer.


Sample Input 2

10
0 0 0 0 0 0 0 0 0 0

Sample Output 2

0

Sample Input 3

5
324097321 555675086 304655177 991244276 9980291

Sample Output 3

805306368

异或的题目,考虑01trie。不妨先把trie建出来。考虑dp。

定义 \(dp_i\) 为如果使用 trie 中点 \(i\) 的子树中的点构成的序列走。也就是如果把点 \(i\) 作为根节点,拎出来一颗 trie 树时的 答案。明显到叶子节点时,\(dp_i=0\)。

假设现在到了点 \(x\),点 \(x\) 位于 01trie 的第 \(i\) 位。

如果他只有一个儿子有值,那么一定有办法把这一位变成0。所以可以直接继承。如果有两个儿子有值,那么这个 \(2^i\) 是逃不掉的了,所以两个儿子中选择较小的那个再加上 \(2^i\) 就行了。

但是这个 dp 中好像有个问题:我们整个序列中所有的数异或上的都是一个数,但是他当中并没有保证这个。是否会出现一个子树中某一位是1,另一个子树中某一位是0的情况呢?其实是会的,但是不影响答案。首先如果只有一个儿子,那就直接继承是没错的。但是如果有两个儿子,此时有 2^i 顶着,这比上面这种情况的影响会大很多。所以真正选择的方案按照两个儿子中 dp 值最小的那个儿子所选择的方案,大的那个不会有影响。

#include<bits/stdc++.h>
using namespace std;
const int N=1.5e5+5;
int a[N],tr[N*30][2],tag[N*30],f[N*30],n,idx(1);
void insert(int x)
{
int u=1;
for(int i=30;i>=0;i--)
{
f[u]=1<<i;
if(!tr[u][x>>i&1])
tr[u][x>>i&1]=++idx;
u=tr[u][x>>i&1];
// printf("%d\n",u);
}
tag[u]=1;
}
int dfs(int x)
{
// printf("%d\n",x);
if(tag[x])
return 0;
if(!x)
return -2000000000;
int p=dfs(tr[x][0]),q=dfs(tr[x][1]);
if(p>q)
swap(p,q);
return max(p+f[x],q);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
insert(a[i]);
}
// printf("%d %d\n",tr[0][0],tr[0][1]);
printf("%d",dfs(1));
return 0;
}

[ABC281F] Xor Minimization的更多相关文章

  1. [CC-MINXOR]XOR Minimization

    [CC-MINXOR]XOR Minimization 题目大意: 有一个长度为\(n\)的数列\(A_{1\sim n}\).\(q\)次操作,操作包含以下两种: 询问\(A_{l\sim r}\) ...

  2. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  3. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  4. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. BZOJ 2115 【Wc2011】 Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  6. xor和gates的专杀脚本

    前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...

  7. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  8. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  9. BC之Claris and XOR

    http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...

  10. 异或链表(XOR linked list)

    异或链表(Xor Linked List)也是一种链式存储结构,它可以降低空间复杂度达到和双向链表一样目的,任何一个节点可以方便的访问它的前驱节点和后继结点.可以参阅wiki 普通的双向链表 clas ...

随机推荐

  1. P1113 杂务 (DAG拓扑排序--DP)

    这是一道拓扑排序的模板题 0 额. 所需的前置知识: 图论相关的基本概念 建图,存图 图的遍历 非常入门的DP 下面进入正文 1 引入 拓扑排序是一类用于处理 DAG(Directed acyclic ...

  2. java实现的类似于sql join操作的工具类,通用递归,最低需要java8

    直接上代码,缺包的自行替换为自己项目中存在的 import java.util.ArrayList; import java.util.Collection; import java.util.Has ...

  3. Jmeter MD5加密及其运用

    常用的几种加密方式 内置函数__MD5加密 参数说明: String to calculate MD5 hash(必填):要加密的字符串 Name of variable in which to st ...

  4. ctypes使用浅谈

    什么是ctypes: ctypes 是 Python 的一个标准库,用于与 C 语言进行交互.它提供了一组工具和函数,可以方便地调用动态链接库(DLL)或共享对象(SO)中的 C 函数,并处理 C 数 ...

  5. git pull 强制覆盖本地代码

    使用git pull更新本地代码,报以下错误: 解决办法如下. 1.备份本地代码 备份,可以考虑直接复制一份项目保存 2.远程覆盖本地 远程覆盖本地容易出现远程和本地冲突的情况 解决办法如下: //1 ...

  6. Solution -「洛谷 P4688」「YunoOI 2016」掉进兔子洞

    Description (Link)[https://www.luogu.com.cn/problem/P4688]. 每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的 ...

  7. 2023-10-04:用go语言,现有一棵无向、无根的树,树中有 n 个节点,按从 0 到 n - 1 编号 给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges , 其中 edge

    2023-10-04:用go语言,现有一棵无向.无根的树,树中有 n 个节点,按从 0 到 n - 1 编号 给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges , 其中 edge ...

  8. android 中service的简单事例

    源码 public class ServiceDemoActivity extends Activity { private static final String TAG = "Servi ...

  9. 虹科干货 | 零售业数智升级不掉队,get数据,get未来!

    电商崛起,传统零售行业危机四伏,全渠道盈利与可持续化成为难点,库存管理这块难啃的"硬骨头"也同样让零售商倍感压力...... 背腹受敌的零售商,如何才能在数字化转型道路上避免利润缩 ...

  10. 解码注意力Attention机制:从技术解析到PyTorch实战

    在本文中,我们深入探讨了注意力机制的理论基础和实际应用.从其历史发展和基础定义,到具体的数学模型,再到其在自然语言处理和计算机视觉等多个人工智能子领域的应用实例,本文为您提供了一个全面且深入的视角.通 ...