首页 > 其他分享 >[ABC281F] Xor Minimization

[ABC281F] Xor Minimization

时间:2022-12-12 18:45:07浏览次数:44  
标签:Sample Xor Minimization int 30 tr ABC281F oplus dp

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;
}

标签:Sample,Xor,Minimization,int,30,tr,ABC281F,oplus,dp
From: https://www.cnblogs.com/mekoszc/p/16976852.html

相关文章

  • BUUCTF之[GWCTF 2019]xxor~~
    老样子,无壳64位。然后丢ida继续分析.在函数列表中找到Main函数,继续分析一开始是让你输入6个int类型的数,并存入到v6数组中在外层的循环中,出现了LODWORD和HIDWORD,这里......
  • buuoj-[MRCTF2020]Xor
    1.winexe32bit无壳2.进入程序无法反汇编去查了百度3.很简单的异或数据是MRCTF{@_R3@1ly_E2_R3verse!}异或它的index就好了str='MSAWB~FXZ:J:`tQJ"N@bpdd}8g'......
  • 一本通1472:The XOR Largest Pair
      将数字看为01串插入字典树, 贪心每次尝试走01串的相反的路#include<iostream>#include<algorithm>#include<cstring>usingnamespacestd;constintN=1e......
  • 异或(xor)的性质
    一点性质(1)xxory=zxxorz=y(2)xor是一个不带进位的二进制加法.实际例子已知\(n\)个同学的学号,现在有一场活动,来了\(n-1\)个同学,他们每个人都把自己的学号写......
  • 『题解』Codeforces 1758B XOR = Average
    Description构造一个\(a\)序列,使\(a_1\oplusa_2\oplusa_3\oplus\cdots\oplusa_n=\dfrac{sum(a)}{n}\)。Solution第一眼看到这道题,我想到的是分情况讨论。......
  • ARC127 Sum of Min of Xor
    可以发现\(a_i\bigoplusb_i\bigoplusa_j\bigoplusb_j\)为\(1\)的位置,是\(a_i\bigoplusa_j\)与\(b_i\bigoplusb_j\)不同的位置。设\(c_i=a_i\bigopl......
  • XOR Sum of Arrays
    section>ProblemStatementForsequences$B=(B_1,B_2,\dots,B_M)$and$C=(C_1,C_2,\dots,C_M)$,eachoflength$M$,consistingofnon-negativeintegers,lettheX......
  • [数学记录]arc137D Prefix Xors
    FWT/高维前缀和入门题。题意:给定一个数列\(a\),每次迭代把原数组替代为前缀异或和数组,求经过\(1-m\)次操作后\(a_n\)的值。\(n\leq10^6\)。首先,无论是手推找规律还......
  • 哈希算法学习笔记 I:XOR hashing
    咕咕中,两天后填坑。CF1175F.TheNumberofSubpermutations求一个序列中是排列的子串数量。CF1746F.Kazaee多组询问,求一个序列的\([l,r]\)段是否为排列。......
  • Complementary XOR
    题目链接题目大意:给你两个字符串只有01组成,你可以选取区间[l,r],对字符串a在区间里面进行异或操作,对字符串b非区间值进行异或操作,问能否将两个字符串变为全0串。如果可以......