Description
today with a small W less than 1s time to complete such a question:
at a given N integers A_1, A_2, ..., A_N for one of two exclusive-or operation, the result is the largest number?
As he complacency, L showed the teacher another problem:
you numbers a1 to 1000 a1000, which is selected from the three digital AI, AJ, AK
(. 1 <= I, J, K <= 1000, and i, j, k different from each other)
meet (ai + aj) xor ak maximum value of
the small W suddenly confused .........
the Input
first line gives digital N, N subsequent lines, each a digital line
Output
If that
the Sample the Input
. 4
. 1
2
. 3
. 4
the Sample Output
. 7
the HINT
(. 1 + 2) =. 7 XOR4
sol: Trie first join all numbers, then the two numbers i and j, and get it in the Trie and the third number k XOR before enumeration. Note that the three numbers to vary, so we went to the front Trie was XOR, from Trie was deleted i and j, after the end of the XOR, then i and j are added back. When deleted, the number of times the +1 each node i and j -1 appears, when inserted, each node i and j occurring.
code show as below:
1 #include<bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 int a[2005],ch[35*1005][2],val[35*1005]; 5 int tol; 6 7 void insert(ll x,ll p) 8 { 9 int u=0; 10 for(int i=31;i>=0;i--) 11 { 12 int v=(x>>i)&1; 13 if(!ch[u][v]) 14 ch[u][v]=tol++; 15 u=ch[u][v]; 16 val[u]+=p; 17 } 18 } 19 20 ll query(ll x) 21 { 22 int u=0; 23 ll ans=0; 24 for(int i=31;i>=0;i--) 25 { 26 int v=(x>>i)&1; 27 if(val[ch[u][v^1]]) 28 u=ch[u][v^1],ans=ans<<1|1; 29 else 30 u=ch[u][v],ans=ans<<1; 31 } 32 return ans; 33 } 34 35 int main() 36 { 37 int T; 38 scanf("%d",&T);//T组数据 39 while(T-- ) 40 { 41 is Tol = . 1 ; 42 is int n-; 43 is Scanf ( " % D " , & n-); // number of each n- 44 is for ( int I = 0 ; I <= n-* 32 ; I ++ ) // build the entire TRIE 45 CH [I] [ 0 ] = CH [I] [ . 1 ] Val = [I] = 0 ; 46 is for ( int I = . 1 ; I <= n-; I ++) // each was added to the digital TRIE 47 { 48 Scanf ( " % D " , A & [I]); 49 INSERT (A [I], . 1 ); // inserted, the number of occurrence of each node increases. 1 50 } 51 is LL Maxx = 0 ; 52 is for ( int = I . 1 ; I <= n-; I ++) // enumeration first number 53 is { 54 is for ( int J = I + . 1 ; J <= n-; J ++) // enumeration second number of 55 { 56 is INSERT ( A [I], - . 1 ); // delete A [i], a [j ] 57 is INSERT (A [J], - . 1 ); // delete operation using the inserting operation equals the number of the node appears -1 58 Maxx = max (Maxx, Query (A [I] + A [J])); 59 // with a [i] + a [j ] in the Trie to XOR number, maximum value 60 // because it is not as a [i] and a [j] themselves, to delete these two numbers, after XOR coupled back 61 is INSERT (A [J], . 1 ); 62 is INSERT (A [I], . 1 ); 63 is } 64 } 65 the printf ( " % LLD \ n- " , Maxx); 66 } 67 return 0 ; 68 }