标签:
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 280 Accepted Submission(s): 158
#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=(1<<29); const double EPS=0.000000001; const double Pi=acos(-1.0); struct Trie { int ch[2]; int siz; void debug() { printf("lch=%2d rch=%2d siz=%2d\n",ch[1],ch[0],siz); } };Trie tr[maxn];int p;int rt; int n,a[maxn]; int newnode() { tr[++p].siz=0; tr[p].ch[0]=tr[p].ch[1]=-1; return p; } void Init() { rt=0; tr[rt].ch[0]=tr[rt].ch[1]=-1; tr[rt].siz=0; p=0; } void Insert(int t) { int k=rt; tr[k].siz++; int c; for(int i=30;i>=0;i--){ if(t&(1<<i)) c=1; else c=0; if(tr[k].ch[c]==-1) tr[k].ch[c]=newnode(); k=tr[k].ch[c]; tr[k].siz++; } } void Delete(int t) { int k=rt; tr[k].siz--; int c; for(int i=30;i>=0;i--){ if(t&(1<<i)) c=1; else c=0; k=tr[k].ch[c]; tr[k].siz--; } } int query(int t) { int k=rt; int res=0; for(int i=30;i>=0;i--){ if(t&(1<<i)){ if(tr[k].ch[0]==-1||tr[tr[k].ch[0]].siz==0){ k=tr[k].ch[1]; } else{ k=tr[k].ch[0]; res|=(1<<i); } } else{ if(tr[k].ch[1]==-1||tr[tr[k].ch[1]].siz==0){ k=tr[k].ch[0]; } else{ k=tr[k].ch[1]; res|=(1<<i); } } } return res; } int main() { freopen("in.txt","r",stdin); int T;cin>>T; while(T--){ scanf("%d",&n); Init(); REP(i,1,n) scanf("%d",&a[i]),Insert(a[i]); int ans=0; REP(i,1,n){ REP(j,i+1,n){ Delete(a[i]);Delete(a[j]); int tmp=query(a[i]+a[j]); ans=max(ans,tmp); Insert(a[i]);Insert(a[j]); } } cout<<ans<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4932956.html