[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 ...
随机推荐
- uni微信小程序隐私协议
最近小程序又新增了个 隐私协议弹窗.需要用户去授权,官网的一些API才能使用.官网地址 功能展示 项目地址:https://ext.dcloud.net.cn/plugin?id=14358 1.ma ...
- Goobye, cnblogs
转 typecho 了,个人网站的客制化程度当然不是 cnblogs 能比得上的. <cirnovsky.cf>
- math 库中常用的数学运算和常量【GO 基础】
〇.关于 math GO 语言的 math 库是一个内置的标准库,其中包含了许多数学函数和常量,用于计算各种数学运算和统计学计算. 日常开发中,计算当然是少不了的,那么今天来梳理下备查. 一.测试示例 ...
- NebulaGraph实战:3-信息抽取构建知识图谱
自动信息抽取发展了几十年,虽然模型很多,但是泛化能力很难用满意来形容,直到LLM的诞生.虽然最终信息抽取质量部分还是需要专家审核,但是已经极大的提高了信息抽取的效率.因为传统方法需要大量时间来完成 ...
- Python基础——二分法、面向过程编程思想、有名函数、lambda、max、_min的应用、sorted排序、map的应用、filter的应用、reduce的应用
文章目录 内容回顾 二分法 伪代码模板 面向过程编程思想 函数式 def用于定义有名函数 lambda用于定义匿名函数 调用匿名函数 匿名函数作用 匿名函数的示范 max的应用 min的应用 sort ...
- linux常见命令(五)
用于文本内容处理的相关命令 sort uniq cut comm diff sort:对文件中数据进行排序,并将结果显示在标准输出上 命令语法:sort [选项] [文件] 选项 选项含义 -m 如果 ...
- ECS搭建FTP服务
实验云账号,创建资源后生成收起 子用户名称: u-7ngohee5@1111971854401817 子用户密码: Jp1Ho5Aj5Ek5Yx1P AK ID: LTAI5tGc6fVMJj44H4 ...
- docker入门加实战—网络
docker入门加实战-网络 我们运行了一些容器,但是这些容器是否能够进行连通呢?那我们就来试一下. 我们查看一下MySQL容器的详细信息: 主要关注,Networks.bridge.IPAddres ...
- nginx学习(基本概念、配置和命令、反向代理、负载均衡、动静分离)
之前都只会照着网上的nginx配置和代码什么的直接拿过来用,但是没系统学习过,所以来系统学习一下nginx内容. 建议服务器不要关闭防火墙,按需开启端口就好,然后云服务器也要设置SSH密钥,安全性高一 ...
- Unity anchoredPosition转localPosition
参考 https://zhuanlan.zhihu.com/p/119442308 在已经有结果的情况下,先捋一下unity对相关字段的注释就能得出很多公式 (rectMinPos表示左下角在父节点坐 ...