[ARC122D] XOR Game
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的更多相关文章
- 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 ...
- [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 ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- BZOJ 2115 【Wc2011】 Xor
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- xor和gates的专杀脚本
前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Xor && 线性基练习
#include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...
- BC之Claris and XOR
http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...
- 异或链表(XOR linked list)
异或链表(Xor Linked List)也是一种链式存储结构,它可以降低空间复杂度达到和双向链表一样目的,任何一个节点可以方便的访问它的前驱节点和后继结点.可以参阅wiki 普通的双向链表 clas ...
- hdu 5661 Claris and XOR
Claris and XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
随机推荐
- 拓展kmp
Smiling & Weeping ---- 我从不觉得暗恋是苦涩的, 对一个人的喜欢藏在眼睛里, 透过它, 世界都变得更好看了. 题目:P5410 [模板]扩展 KMP(Z 函数) - 洛谷 ...
- c++中的数论知识
写在开头:word的公式打不上来,只能截图了 一.组合数学 (1) 加法定理与乘法原理 加法原理:做一件事情,完成它可以有n类办法,在第一类办法中有m1种不同的方法,在第二类办法中有m2种不同的方法, ...
- No module named virtualenvwrapper 虚拟环境报错
No module named virtualenvwrapper 虚拟环境报错 安装虚拟环境命令 sudo pip install virtualenv sudo pip install virtu ...
- Http协议之libcurl实现
一.libcurl简介 libcurl是一个跨平台的网络协议库,支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议.libcurl同样支持 ...
- 其它——DevOps简介
文章目录 DevOps简介 DevOps的概念 历史变革 好处是什么? 为什么DevOps会兴起? 实现DevOps需要什么? DevOps的采用现状 DevOps简介 DevOps 是一个完整的面向 ...
- python第6章 学习笔记
# 第6章 学习笔记## 简介 Python代码在执行时是按照自上向下顺序执行的. 通过流程控制语句,可以改变程序的执行顺序,也可以让指定的程序反复执行多次 流程控制语句分成两大类:条件判断语句,循环 ...
- 栈和堆的区别、FreeRTOS 中的任务栈
栈和堆的区别.FreeRTOS 中的任务栈 01 堆和栈的概念 堆 功能 堆是一块用于动态分配内存的区域,用于存储程序运行时动态创建的对象.堆的大小可以在程序运行时动态调整. 特点 堆的分配和释放是由 ...
- Oracle 高可用 阅读笔记
1 个人理解概述 1.1 Oracle dg Oracle Data Guard通过从主数据库传输redo data,然后将apply redo到备用数据库,自动维护每个备用数据库.DG分为3个 ...
- 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(12) -- 使用代码生成工具Database2Sharp生成WPF界面代码
1.代码生成工具Database2Sharp生成WPF界面代码 WPF应用端的基础接口,和Winform端.Vue3+ElementPlus前端一样,都是调用SqlSugar开发框架中的相关业务接口, ...
- 04-23: dataclasses使用方法
vehicle_seeds: List[int] = dataclasses.field(default_factory=list) dataclasses 模块提供了一种简洁的方式来定义Python ...