HDU5536 Chip Factory
Chip Factory
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 6984 Accepted Submission(s): 3129
Problem Description
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces n chips today, the i-th chip produced this day has a serial number si.
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
maxi,j,k(si+sj)⊕sk
which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
Input
The first line of input contains an integer T indicating the total number of test cases.
The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤109
There are at most 10 testcases with n>100
The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤109
There are at most 10 testcases with n>100
Output
For each test case, please output an integer indicating the checksum number in a line.
Sample Input
2
3
1 2 3
3
100 200 300
Sample Output
6
400
Source
Recommend
题解:01字典树.
参考代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x*f; } int const maxn=1e5+10; int a[maxn]; struct Trie_01{ int root,tot,nxt[maxn][2],cnt[maxn],end[maxn]; int Newnode() { memset(nxt[tot],-1,sizeof(nxt[tot])); cnt[tot]=0; end[tot]=0; return tot++; } void Init() { tot=0; root=Newnode(); } void Insert(int x) { int p=root; cnt[p]++; for(int i=31;i>=0;--i) { int idx=(1&(x>>i)); if(nxt[p][idx]==-1) nxt[p][idx]=Newnode(); p=nxt[p][idx]; ++cnt[p];//可能会有重复的 } end[p]=x; } void Delete(int x)//删除一个数x { int p=root; --cnt[p]; for(int i=31;i>=0;i--) { int idx=(1&(x>>i)); p=nxt[p][idx]; --cnt[p]; } } int QueryMax(int x)//求xor最大值 { int p=root; for(int i=31;i>=0;--i) { int idx=(1&(x>>i)); if(idx==0) { if(nxt[p][1]!=-1&&cnt[nxt[p][1]]) p=nxt[p][1]; else p=nxt[p][0]; } else { if(nxt[p][0]!=-1&&cnt[nxt[p][0]]) p=nxt[p][0]; else p=nxt[p][1]; } } return (x^end[p]); } int QueryMin(int x)//求xor最小值 { int p=root; for(int i=31;i;--i) { int idx=(1&(x>>i)); if(idx==1) { if(nxt[p][1]!=-1&&cnt[nxt[p][1]]) p=nxt[p][1]; else p=nxt[p][0]; } else { if(nxt[p][0]!=-1&&cnt[nxt[p][0]]) p=nxt[p][0]; else p=nxt[p][1]; } } return (x^end[p]); } }trie; int main() { int T=read(); while(T--) { trie.Init(); int n,ans=0; n=read(); for(int i=0;i<n;++i) { a[i]=read(); trie.Insert(a[i]); } for(int i=0;i<n;++i) { for(int j=i+1;j<n;++j) { trie.Delete(a[i]); trie.Delete(a[j]); ans=max(ans,trie.QueryMax(a[i]+a[j])); trie.Insert(a[i]); trie.Insert(a[j]); } } printf("%d\n", ans); } return 0; }