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. 一文了解 history 和 react-router 的实现原理

    我们是袋鼠云数栈 UED 团队,致力于打造优秀的一站式数据中台产品.我们始终保持工匠精神,探索前端道路,为社区积累并传播经验价值. 本文作者:霜序 前言 在前一篇文章中,我们详细的说了 react-r ...

  2. TCP的可靠性之道:确认重传和流量控制

    TCP 全称为 Transmission Control Protocol(传输控制协议),是一种面向连接的.可靠的.基于字节流的传输层通信协议,其中可靠性是相对于其他传输协议的优势点.TCP 为了确 ...

  3. OpenSSH版本升级漏洞修复问题

    Hi, I'm @Merbelue 大家好,这篇为大家介绍二进制方式对OpenSSH版本升级,在生产环境中可用于解决版本升级.漏洞修复等. @ 目录 1.环境 2.安装telnet 2.1.检查是否安 ...

  4. 《深入理解Java虚拟机》读书笔记:方法调用

      方法调用并不等同于方法执行,方法调用阶段唯一的任务就是确定被调用方法的版本(即调用哪一个方法),暂时还不涉及方法内部的具体运行过程.在程序运行时,进行方法调用是最普遍.最频繁的操作,但前面已经讲过 ...

  5. HDU 1171 0-1背包

    最近感觉DP已经完全忘了..各种爆炸,打算好好复习一发,0-1背包开始 Big Event in HDU Problem Description Nowadays, we all know that ...

  6. 火山引擎DataLeap的数据血缘用例与设计概述

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群   数据血缘描述了数据的来源和去向,以及数据在多个处理过程中的转换.数据血缘是组织内使数据发挥价值的重要基础能力. ...

  7. 探秘公有IP地址与私有IP地址的区别及其在路由控制中的作用

    引言 IP地址是互联网通信中至关重要的组成部分.虽然在前一章节我们讲解了IP一些基础知识,但在我们日常生活中,我们经常听到公有IP地址和私有IP地址这两个术语.那么,公有IP地址和私有IP地址有何区别 ...

  8. PDFPlumber使用入门

    目录 背景 教程开始 应用场景 安装 命令行使用 可选参数 Python包 简单样例 读取PDF pdfplumber.PDF类 pdfplumber.Page类 对象(Object) chars / ...

  9. xgo多线程

    import threading import time #导入xgoedu from xgoedu import XGOEDU from xgolib import XGO #导入xgolib  # ...

  10. android开发阶段性技能

    一.初级 1. 拥有娴熟的Java基础,理解设计模式,比如OOP语言的工厂模式要懂得. 2. 掌握Android UI控件.Android Java层API相关使用. 迈向中级,最好再次更新下Java ...