Problem Statement

There are $2N$ integers written on a blackboard. The $i$-th integer is $A_i$.

Alice and Bob will play a game consisting of $N$ rounds.
In each round, they do the following:

  • First, Alice chooses an integer on the blackboard and erases it. Let $x$ be the integer erased here.
  • Second, Bob chooses an integer on the blackboard and erases it. Let $y$ be the integer erased here.
  • Finally, write the value $x \oplus y$ on a notebook, where $\oplus$ denotes the bitwise XOR.

In the end, all the integers on the blackboard will be erased, and the notebook will have $N$ integers written on it.
The greatest integer written on the notebook will be the score of the game.
Alice wants to maximize this score, while Bob wants to minimize it.
Find the score of the game when both players play optimally under their objectives.

Constraints

  • $1 \leq N \leq 2 \times 10^5$
  • $0 \leq A_i < 2^{30}$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\cdots$ $A_{2N}$

Output

Print the answer.


Sample Input 1

2
0 1 3 5

Sample Output 1

4

Below is one possible progress of the game (it may contain suboptimal choices).

  • Round $1$:

    • Alice chooses $A_1=0$.
    • Bob chooses $A_3=3$.
    • They write $0 \oplus 3=3$ on the notebook.
  • Round $2$:

    • Alice chooses $A_4=5$.
    • Bob chooses $A_2=1$.
    • They write $5 \oplus 1=4$ on the notebook.
  • The score of the game is $\max(3,4)=4$.


Sample Input 2

2
0 0 0 0

Sample Output 2

0

Sample Input 3

10
974654030 99760550 750234695 255777344 907989127 917878091 818948631 690392797 579845317 549202360 511962375 203530861 491981716 64663831 561104719 541423175 301832976 252317904 471905694 350223945

Sample Output 3

268507123

首先发现 Alice 是个废物,因为 Bob 找到原图一个匹配后, Alice 选什么 Bob 就选对应的那个即可。

所以现在要找到一个匹配,使得每条边两边的异或最大值最小。

考虑使用 trie 来分析。

对于某一个节点 \(x\) 的子树,如果他的左子树和右子树大小都是偶数,那么两个子树都可以自己完成匹配,可以递归到里面计算。

否则,就让他们自己匹配完之后,两个子树各自选出一个异或。所以可以递归计算两个子树中各选一个数异或的最小值。

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int n,a[N],ret=0,tr[N*30][2],mx=-1,tg[N*30],idx,s[30],sz[N*30];
void insert(int x)
{
int u=0;
for(int i=29;~i;--i)
{
if(!tr[u][x>>i&1])
tr[u][x>>i&1]=++idx;
u=tr[u][x>>i&1];
sz[u]++;
}
tg[u]=x;
}
int ask(int x,int y)
{
if(!x||!y)
return INT_MAX;
if(tg[x])
return tg[x]^tg[y];
if(!tr[x][0]&&!tr[y][1])
return ask(tr[x][1],tr[y][0]);
if(!tr[x][1]&&!tr[y][0])
return ask(tr[x][0],tr[y][1]);
return min(ask(tr[x][0],tr[y][0]),ask(tr[x][1],tr[y][1]));
}
void solve(int x)
{
if(sz[tr[x][0]]&1)
ret=max(ret,ask(tr[x][0],tr[x][1]));
else
{
if(tr[x][0])
solve(tr[x][0]);
if(tr[x][1])
solve(tr[x][1]);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n*2;i++)
{
scanf("%d",a+i);
for(int j=0;j<30;j++)
if(a[i]>>j&1)
s[j]++;
}
for(int i=1;i<=n*2;i++)
insert(a[i]);
solve(0);
printf("%d",ret);
}

[ARC122D] XOR Game的更多相关文章

  1. ARC122D XOR Game(博弈论?字典树,贪心)

    题面 ARC122D XOR Game 黑板上有 2 N 2N 2N 个数,第 i i i 个数为 A i A_i Ai​. O I D \rm OID OID(OneInDark) 和 H I D ...

  2. [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 ...

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

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

  4. BZOJ 2115 【Wc2011】 Xor

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

  5. xor和gates的专杀脚本

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

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

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

  7. Xor && 线性基练习

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

  8. BC之Claris and XOR

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

  9. 异或链表(XOR linked list)

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

  10. hdu 5661 Claris and XOR

    Claris and XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

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

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

  2. Ceph-介绍

    Ceph架构简介及使用场景介绍 一.Ceph简介 Ceph是一个统一的分布式存储系统,设计初衷是提供较好的性能.可靠性和可扩展性. 二.Ceph特点 1.高性能 - 采用CRUSH算法,数据分布均衡, ...

  3. DevSecOps之应用安全测试工具及选型

    上篇文章,有同学私信想了解有哪些DevSecOps工具,这里整理出来,供大家参考(PS: 非专业安全人士,仅从DevOps建设角度,给出自己见解) 软件中的漏洞和弱点很常见:84%的软件漏洞都是利用应 ...

  4. 【krpano】 ASP点赞插件

    简述 这是一个Asp版krpano点赞案例,运用asp+xml读写.存储数据,结合krpano代码实现的功能:现将案例上传网站供大家学习研究,希望对大家有所帮助. 功能 用户在网页可点赞后显示已点赞数 ...

  5. 使用MySQL存储过程提高数据库效率和可维护性

    MySQL 存储过程是一种强大的数据库功能,它允许你在数据库中存储和执行一组SQL语句,类似于编程中的函数.存储过程可以大幅提高数据库的性能.安全性和可维护性.本文将详细介绍MySQL存储过程的使用. ...

  6. Solution -「BZOJ 3779」重组病毒

    Description Link. Given is a tree. Every node initially has a color which is different from others'. ...

  7. 如何在Nuxt3.0中使用MongoDB数据库

    如何在Nuxt3.0中使用MongoDB数据库 一.介绍 Nuxt.js 是一个基于 Vue.js 的开源框架,用于构建服务端渲染 (Server-Side Rendering, SSR) 或静态生成 ...

  8. 熟练掌握并充分利用CSS3的新特性,更新完毕。

    1.1  尝试新颖的CSS3特性 首先,我们来看一个具体的案例.  https://code.juejin.cn/pen/7277536985772720139   1.2  CSS3新特性简介和浏览 ...

  9. NineData SQL 窗口支持深色模式,让程序员不再怕长期用眼!

    您有没有尝试过被明亮的显示器闪瞎眼的经历? 在夜间或低光环境下,明亮的界面会导致许多用眼健康问题,例如长时间使用导致的眼睛疲劳.干涩和不适感,同时夜间还可能会抑制褪黑素分泌,给您的睡眠质量带来影响. ...

  10. pbjs 无法编码 bytes 类型数据问题的解决方案

    问题背景 之前写过一篇<使用脚本收发 protobuf 协议数据>,通过 pbjs 命令可以将 protobuf 二进制数据转换为 json: > pbjs msg.proto -- ...