当前位置:网站首页>[28. Maximum XOR pair]
[28. Maximum XOR pair]
2022-07-25 01:03:00 【Little silly bird_ coding】
summary
- The largest XOR pair is in a given number , Find two numbers to make , The result of exclusive or of these two numbers is the largest .
- It is generally used
Violence lawandDictionary treeMethods .
Violent writing
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
}
int res = 0;
for (int i = 0; i < n; i ++)
{
for (int j = 0; j < i; j ++)
{
res = max(res, a[i] ^ a[j]);
}
}
cout << res << endl;
return 0;
}
Dictionary tree optimization
The building process
- Start from the top , Because in the XOR process
( Dissimilarity is 1, Same as 0), We try to satisfyHigh maximum.

Program realization
void insert(int x)
{
int p = 0; // The root node
for (int i = 30; i >= 0; i --) // Start with the largest bit
{
int u = x >> i & 1; // take X Of the i What is the binary number of bits x>>k&1( The front template )
if (!son[p][u]) son[p][u] = ++ idx; // If no child node is found in the insertion , Take this road
p = son[p][u]; // The pointer points to the next layer
}
}
Find the maximum XOR result
Exclusive or
- Both numbers are converted to binary , If two bits are the same, XOR is followed by 0, Different XOR is 0.( Dissimilarity is 1, Same as 0)
Realization
- When there is no number of root nodes , Let's first insert a number
- When inserting the second number again , You should XOR with the first number inserted first .
- When inserting the third number again , It should be XOR with all numbers inserted before , Get the maximum .
How to find the largest
- For everyone , For example, the current highest level is 1, In order to maximize the post XOR value , We need to find the highest position is 0 Exclusive or of , In this way, the final result is the biggest
- If the number after XOR is 1, best , If not , It can only be 0.
Code implementation
int query(int x)
{
int p = 0, res = 0; // use res To access the number represented by the current path
for (int i = 30; i >= 0; i --) // Start with the largest bit
{
int u = x >> i & 1;
if (son[p][!u]) // If the current layer has a corresponding different number
{
p = son[p][!u]; //p The pointer points to different numbers of addresses
res = res * 2 + !u; //*2 Represents a shift to the left , Add the next one
} // for example 001 * 2 = 0010 The next one is 1,
// 0010 + 1 = 00101
else
{
p = son[p][u];
res = res * 2 + u;
}
}
return res;
}
subject

Code
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010, M = 31 * N;
int son[M][2]; //M Number of representative nodes (M Represents how long a binary string of numbers can go ),2 There are only two branches , One is 0, One is 1
int a[N];
int idx;
void insert(int x)
{
int p = 0; // The root node
for (int i = 30; i >= 0; i --) // Start with the largest bit
{
int u = x >> i & 1; // take X Of the i What is the binary number of bits x>>k&1( The front template )
if (!son[p][u]) son[p][u] = ++ idx; // If no child node is found in the insertion , Take this road
p = son[p][u]; // The pointer points to the next layer
}
}
int query(int x)
{
int p = 0, res = 0; // use res To access the number represented by the current path
for (int i = 30; i >= 0; i --) // Start with the largest bit
{
int u = x >> i & 1;
if (son[p][!u]) // If the current layer has a corresponding different number
{
p = son[p][!u]; //p The pointer points to different numbers of addresses
res = res * 2 + !u; //*2 Represents a shift to the left , Add the next one
} // for example 001 * 2 = 0010 The next one is 1,
// 0010 + 1 = 00101
else
{
p = son[p][u];
res = res * 2 + u;
}
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
int res = 0;
for (int i = 0; i < n; i ++)
{
insert(a[i]);
int t = query(a[i]); //query(a[i]) Looking for a[i] The maximum and or value of the value
res = max(res, a[i] ^ t);
}
printf("%d\n", res);
return 0;
}
边栏推荐
- Number of palindromes in question 5 of C language deduction (two methods)
- Summary of MATLAB basic grammar
- VC hesitates to invest in Henan
- Example analysis of recombinant monoclonal antibody prosci CD154 antibody
- Call camera photo album / upload / scan code in uniapp
- About the difference between for... In and for... Of and object. Keys()
- paddlepaddle论文系列之Alexnet详解(附源码)
- Simple use of mongodb database
- 7.15 - daily question - 408
- Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos
猜你喜欢

7.18 - daily question - 408

Implementing DDD based on ABP -- domain logic and application logic

Amd epyc 9654 Genoa CPU cache test exposure L1 bandwidth up to 30tb/s

Pychart exits pytest mode (run pytest in mode)

7.16 - daily question - 408

Latest information of 2022 cloud computing skills competition
![Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation](/img/e1/51f750d355b248ab84e10f0e134271.png)
Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation

What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?

The IPO of Tuba rabbit was terminated: the annual profit fell by 33%, and Jingwei Sequoia was the shareholder

Password input box and coupon and custom soft keyboard
随机推荐
If real-time intersection with line segments in online CAD drawings is realized
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
The number of palindromes in question 9 of C language deduction. Two pointer array traversal method
A string "0" was actually the culprit of the collapse of station b
unresolved external symbol [email protected] resolvent
494. Target sum · depth first search · knapsack problem
Codeworks round 649 (Div. 2) ABC problem solution
How to better use the touchpad of notebook
asp rs.open sql,conn,3,1中3,1代表什么?
Grafana connection tdengine reports an error 535
asp adodb.stream对象的方法属性
第三章 内核开发
ROS manipulator movelt learning notes 3 | kinect360 camera (V1) related configuration
Is qiniu Business School reliable? Is it safe to open Huatai account recommended by the lecturer
Add the two numbers in the linked list of the second question of C language. Ergodic method
Current events on July 20
Tool use of rookie tutorial -- View subclass (implementation class) class diagram in idea
Automated test series selenium three kinds of waiting for detailed explanation
How to implement the server anti blackmail virus system is a problem we have to consider
自动化测试系列-Selenium三种等待详解