当前位置:网站首页>(板子)AcWing 143.最大异或对
(板子)AcWing 143.最大异或对
2022-07-15 20:27:00 【咸蛋_dd】
在给定的 NN 个整数 A1,A2……ANA1,A2……AN 中选出两个进行 xorxor(异或)运算,得到的结果最大是多少?
输入格式
第一行输入一个整数 NN。
第二行输入 NN 个整数 A1A1~ANAN。
输出格式
输出一个整数表示答案。
数据范围
1≤N≤1051≤N≤105,
0≤Ai<2310≤Ai<231
输入样例:
3
1 2 3
输出样例:
3#include <bits/stdc++.h>
using namespace std;
long long int b[35];
int son[3000010][3];
int idx=0;
long long int ans=0;
int n;
void Insert(int x)
{
int p=0;
for(int i=30;i>=0;i--)
{
//printf("%d %d\n",x>>i&1,son[p][x>>i&1]);
if(son[p][x>>i&1]==0)
son[p][x>>i&1]=++idx;
p=son[p][x>>i&1];
}
}
void ques(int x)
{
long long int sum=0;
int p=0;
for(int i=30;i>=0;i--)
{
if(x>>i&1==1)
{
if(son[p][0]!=0)
{
sum+=b[i];
p=son[p][0];
}
else
{
p=son[p][1];
}
}
else
{
if(son[p][1]!=0)
{
sum+=b[i];
p=son[p][1];
}
else
p=son[p][0];
}
}
ans=max(ans,sum);
}
int main()
{
memset(son,0,sizeof(son));
long long int a[100010];
b[0]=1;
/*for(int i=31;i>=0;i--)
{
printf("%d",2>>i&1);
}
printf("\n");*/
for(int i=1;i<=30;i++)
{
b[i]=b[i-1]*2;
}
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
Insert(a[i]);
}
for(int i=1;i<=n;i++)
{
ques(a[i]);
}
printf("%lld\n",ans);
return 0;
}
边栏推荐
- GD32F4xx IAP 升级思路
- 微服务学习
- Leetcode-240-search two-dimensional matrix II
- Development history of digital twin power grid
- Access control settings
- The application of digital twins in cities
- Codeforces Round #802 B. Palindromic Numbers
- 数字孪生在城市中的应用
- npm install安装报错:gyp info it worked if it ends with ok如何解决
- Codeforces Round #802 A. Optimal Path
猜你喜欢

Leetcode-62-different paths

Leetcode-128-longest continuous sequence
Shiro integrates redis to realize distributed session processing

nacos win10单机启动命令
![Microservice architecture | configuration center - [config]](/img/0f/ddba593a01c72dcf68c7d8e923fab6.png)
Microservice architecture | configuration center - [config]

Offline installation of MySQL 5.7 for Linux

Educational Codeforces Round 131 A - D

【空间&单细胞组学】第2期:联合单细胞和bulk转录组鉴定了结直肠癌中两种上皮肿瘤细胞状态,并完善了CMS分型
![2022-7-15 Leetcode 151. Reverse the words in the string - [split from back to front]](/img/46/6bb2c4e59b2328482b92619783f0bd.png)
2022-7-15 Leetcode 151. Reverse the words in the string - [split from back to front]

C语言爱心代码大全(2022表白神器)
随机推荐
Codeforces Round #804 B Almost Ternary Matrix
[ArcGIS creates a small picture frame of islands and nine segment lines in the South China Sea]
数字孪生在城市中的应用
【物联网】WiFi基础知识 (二)【看评论区领取资料】
Centos7.2 install mysql5.7 through RPM package
模拟卷Leetcode【普通】1894. 找到需要补充粉笔的学生编号
华为云服务器云数据库创建只读过程
Can communication (2) - can communication protocol layer
LeetCode-83-删除链表中的重复元素
JUC joint contracting - semaphore
MySQL logs: undo log, redo log, binlog
Unity shader - cginclude file cginc
2022-7-15 Leetcode 151.颠倒字符串中的单词 —— 【从后往前分割】
JUC joint contracting - cyclicbarrier
Mysql——ER模型
Scripting rules and variable definitions
项目管理系统选择有哪些需要注意的点?
Centos7 installs MySQL 5.7 through Yum and enables remote access
君乐宝要IPO了,资本却笑不出来
面试基础题