【LC周赛-371】 D. Trie树求最大异或对
题意
- 给一个数组,求两个数满足|x-y|<=min(x,y)的异或最大值。
题解
- 从|x-y|<=min(x,y)知道,每个y可以考虑的x范围是 y / 2 <= x < y;
- 然后Trie树实现更优复杂度内,从窗口获得最大异或值
- 思路就是高位依次取值,具体看代码吧
代码
const int N = 1e6 + 5;
class Solution {
public:
int trie[N][2];
int num[N];
int cnt = 1;
void trie_insert(int x) {
int u = 0;
for(int i = 20; i >= 0; -- i) {
int v = (x >> i) & 1;
if(!trie[u][v]) trie[u][v] = ++ cnt;
u = trie[u][v];
num[u] ++;
}
}
void trie_del(int x) {
int u = 0;
for(int i = 20; i >= 0; -- i) {
int v = (x >> i) & 1;
u = trie[u][v];
num[u] --;
}
}
int trie_XORmax(int x) {
int u = 0, res = 0;
for(int i = 20; i >= 0; -- i) {
int v = (x >> i) & 1;
if(num[trie[u][!v]]) {
u = trie[u][!v];
res = (res << 1) + 1;
} else {
u = trie[u][v];
res = (res << 1) + 0;
}
}
return res;
}
int maximumStrongPairXor(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
int l = 0, r = 1, res = 0;
trie_insert(nums[0]);
while(r < n) {
while(l < n && nums[l] * 2 < nums[r]) trie_del(nums[l ++]);
res = max(res, trie_XORmax(nums[r]));
trie_insert(nums[r ++]);
}
return res;
}
};