[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 ...
随机推荐
- ORM查询一个表中有两个字段相同时,只获取某个值最大的一条
Table表如下: 获取表中name和hex值相同时age最大的那一条 ORM写法,两次查询 ids = table.values('name', 'age').annotate(id=Max('id ...
- 【krpano】KRPano自动缩略图分组插件
该插件可以展示场景缩略图,并支持场景分组. 下载地址:http://pan.baidu.com/s/1dFj7v0l 使用说明: 插件共有两个文件,auto_thumbs.xml和tooltip.xm ...
- oracle优化-分页查询新认识
在写这篇文章之前,对分页查询的认识就是经典的三层嵌套:第①层:获得需要的数据,第②层:用rownum限制展示数据的上限,第③层:用rownum的别名rn限制展示数据的下限. 在生产中遇见一个两层嵌套满 ...
- 在线问诊 Python、FastAPI、Neo4j — 生成 Cypher 语句
目录 构建节点字典 构建Cypher CQL语句 Test 这边只是为了测试,演示效果和思路,实际应用中,可以通过NLP构建CQL 接上一篇的问题分类 question = "请问最近看东西 ...
- 个人理解strcpy
char * strcpy(char *dst,const char *src) { if((dst==NULL)||(src==NULL)) return NULL; char *ret = dst ...
- 8. 用Rust手把手编写一个wmproxy(代理,内网穿透等), HTTP改造篇之HPACK原理
用Rust手把手编写一个wmproxy(代理,内网穿透等), HTTP改造篇之HPACK原理 项目 ++wmproxy++ gite: https://gitee.com/tickbh/wmproxy ...
- Tarjan强连通分量详解
1.简介: 在阅读下列内容之前,请务必了解 图论相关概念 中的基础部分. 强连通的定义是:有向图 G 强连通是指,G 中任意两个结点连通. 强连通分量(Strongly Connected Compo ...
- P6346 [CCO2017] 专业网络 & CF1251E1 Voting(Easy Version)
analysis 这个题目我们可以考虑用贪心来做. 我们不难看出来,这个题目是要让我们推出这么个结论:花小钱,办大人. 整体贪心的思路就出来了,然后就是实现部分. 因为我们认识的人随便是谁都可以.所以 ...
- jmeter生成HTML性能测试报告(非GUI的命令)
非GUI的命令(在cmd执行即可 不需要打开jmeter) 使用命令:jmeter -n -t [jmx file] -l [jtl file] -e -o [report path ...
- 关于fdisk -l
摘取:https://blog.csdn.net/digitalkee/article/details/104226846 # fdisk -l Disk /dev/hda: 160.0 GB, 16 ...