题目大意:
在给定的 (N) 个整数 (A_1,A_2……A_N) 中选出两个进行 (xor)(异或)运算,得到的结果最大是多少?
正文:
01trie 板子。
代码:
const int N = 2e6 + 10;
inline ll READ()
{
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * f;
}
int n, k;
ll a[N];
struct Trie
{
int ch[N][2];
ll siz[N], tot;
Trie(){tot = 1;}
void ins(ll val)
{
int u = 1;
for (ll i = 31ll; ~i; --i)
{
bool k = (val >> i) & 1; siz[u]++;
if (!ch[u][k]) ch[u][k] = ++tot;
u = ch[u][k];
}
siz[u] ++;
return;
}
ll query(ll val, int n)
{
int u = 1;ll tmp = 0;
for (int i = 31; ~i; --i)
{
bool k = (val >> i) & 1;
if(!ch[u][k ^ 1]) u = ch[u][k];
else if (n <= siz[ch[u][k ^ 1]]) u = ch[u][k ^ 1], tmp |= 1ll << i;
else n -= siz[ch[u][k ^ 1]], u = ch[u][k];
// if (val == 1036879) printf ("%lld, %d, t[][]={%d, %d}
", tmp, u, ch[u][0], ch[u][1]);
}
return tmp;
}
}t;
ll ans = 0;
int main()
{
n = READ();
// t.ins(0);
for (int i = 1; i <= n; i++)
a[i] = READ(), t.ins(a[i]);
for (ll i = 1, x; i <= n; i++)
ans = max(ans, x = t.query(a[i], 1));
printf ("%lld
", ans);
return 0;
}