题意:给出N个数,选出三个下标不同的数,令(Ai+Aj)^Ak最大。
范围:N<=1000
解法:首先想到两数异或和最大可以贪心+Trie树搜索解决,此时只需要将所有数化为01串加入Trie树,并且枚举i,j,在trie树中贪心得到一个异或或最大的即可(要先把i,j从Trie树中删除,计算完后再insert)
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define NN 100100
int n,a[NN],tot;
struct node{
int cot;
int a[2];
void init(){
a[0]=a[1]=0;
cot=0;
}
}t[110110];
void init(){
tot=0;
t[0].init();
}
int b[55];
void insert(int x){
int y=1;
rep(i,1,31){
if(x&y)b[i]=1;
else b[i]=0;
y<<=1;
}
reverse(b+1,b+32);
x=0;
t[x].cot++;
rep(i,1,31){
if(t[x].a[b[i]])x=t[x].a[b[i]];
else{
t[++tot].init();
t[x].a[b[i]]=tot;
x=tot;
}
t[x].cot++;
}
}
void dele(int x){
int y=1;
rep(i,1,31){
if(x&y)b[i]=1;
else b[i]=0;
y<<=1;
}
reverse(b+1,b+32);
x=0;
t[x].cot--;
rep(i,1,31){
x=t[x].a[b[i]];
t[x].cot--;
}
}
int find(int x){
int y=1;
rep(i,1,31){
if(x&y)b[i]=1;
else b[i]=0;
y<<=1;
}
reverse(b+1,b+32);
x=0;
int ans=0;
rep(i,1,31){
if(t[x].cot>0){
int l=t[x].a[b[i]^1];
int r=t[x].a[b[i]];
if(l&&t[l].cot>0){
x=l;
ans=(ans<<1)|(b[i]^1);
}
else if(r&&t[r].cot>0){
x=r;
ans=(ans<<1)|(b[i]);
}
}
}
return ans;
}
int main()
{
tdata{
init();
scanff(n);
rep(i,1,n)scanff(a[i]),insert(a[i]);
int ans=0;
rep(i,1,n){
dele(a[i]);
rep(j,i+1,n){
dele(a[j]);
int x=a[i]+a[j];
int y=find(x);
ans=max(ans,x^y);
insert(a[j]);
}
insert(a[i]);
}
printf("%d\n",ans);
}
return 0;
}