The XOR Largest Pair
思路
树板子题,直利用异或的性质,在树上遍历就行了。
代码
/* Author : lifehappy */ #include <bits/stdc++.h> using namespace std; const int N = 5e6 + 10; int trie[N][2], a[N], n, tot; void add(int x) { int rt = 0; for(int i = 30; i >= 0; i--) { int now = x >> i & 1; if(!trie[rt][now]) trie[rt][now] = ++tot; rt = trie[rt][now]; } } int query(int x) { int rt = 0, ans = 0; for(int i = 30; i >= 0; i--) { int now = x >> i & 1; if(trie[rt][now ^ 1]) { ans |= 1 << i; rt = trie[rt][now ^ 1]; } else { rt = trie[rt][now]; } } return ans; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for(int i = 1; i <= n; i++) { cin >> a[i]; add(a[i]); } int ans = 0; for(int i = 1; i <= n; i++) { ans = max(ans, query(a[i])); } cout << ans << "\n"; return 0; }