XOR maximum of

XOR maximum of

Given number n, as the i-th \ (a_i \) , Q taken from any two numbers, XOR, the maximum value of the results obtained, \ (n-\ Leq. 5 ^ 10 \) .

solution

First, the idea of ​​violence is to enumerate every number, then another number to determine the question now is how to quickly determine another number.

Binary arithmetic problems, binary split, for every, especially from the highest consideration, choose the number of pile as possible, so that the highest level is not zero, in a pile from a few selected number of re-election bunch, let time high as possible is not zero, kept narrow the scope of a collection, you can get our answer.

The question then becomes how quickly retrieve each corresponding collection, so that we can take advantage of a trie to do, because it can query a bit corresponding to a character string, so we put numbers as 01 strings, adding trie tree, according to the above mentioned idea, the time complexity \ (O (nlog (n-)) \) .

Reference Code:

#include <iostream>
#include <cstdio>
#define il inline
#define ri register
#define Size 3100000
using namespace std;
int trie[Size][2],tot,
    a[100005];
il int ask(int);
il void insert(int),
    read(int&);
template<class free>
il free Max(free,free);
int main(){
    int n,ans(0);read(n);
    for(int i(1);i<=n;++i)
        read(a[i]),insert(a[i]);
    for(int i(1);i<=n;++i)
        ans=Max(ans,ask(a[i]));
    printf("%d",ans);
    return 0;
}
template<class free>
il free Max(free a,free b){
    return a>b?a:b;
}
il void read(int &x){
    x^=x;ri char c;while(c=getchar(),c<'0'||c>'9');
    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}
il int ask(int x){int p(0),ans(0);
    for(int i(30);i>=0;--i)
        if(trie[p][x>>i&1^1])
            p=trie[p][x>>i&1^1],ans|=1<<i;
        else p=trie[p][x>>i&1];
    return ans;
}
il void insert(int x){int p(0);
    for(int i(30);i>=0;--i){
        if(!trie[p][x>>i&1])
            trie[p][x>>i&1]=++tot;
        p=trie[p][x>>i&1];
    }
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11248058.html
XOR