Chip Factory(01字典树)

Chip Factory

http://acm.hdu.edu.cn/showproblem.php?pid=5536

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 280    Accepted Submission(s): 158

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:
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.

1T1000
3n1000
0si109
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
 
用num标记该点是否被删除,剩下的就是01字典树的模板了
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 #include<queue>
 7 #define maxn 1005
 8 using namespace std;
 9 int tol;
10 long long val[maxn*35];
11 int tree[maxn*35][2];
12 int num[maxn*32][2];
13 long long a[maxn];
14 
15 void init(){
16     tol=1;
17     tree[0][0]=tree[0][1]=0;
18     memset(num,0,sizeof(num));
19 }
20 
21 void add(long long x,int k){
22     int u=0;
23     for(int i=32;i>=0;i--){
24         int v=(x>>i)&1;
25         num[u][v]+=k;
26         if(!tree[u][v]){
27             tree[tol][0]=tree[tol][1]=0;
28             val[tol]=-1;
29             tree[u][v]=tol++;
30         }
31         u=tree[u][v];
32     }
33     val[u]=x;
34 }
35 
36 long long query(long long n){
37     int u=0;
38     for(int i=32;i>=0;i--){
39         int v=(n>>i)&1;
40         if(num[u][v^1]) u=tree[u][v^1];
41         else u=tree[u][v];
42     }
43     return val[u];
44 }
45 
46 int main(){
47     int t;
48     scanf("%d",&t);
49     while(t--){
50         int n;
51         scanf("%d",&n);
52         init();
53         for(int i=1;i<=n;i++){
54             scanf("%lld",&a[i]);
55             add(a[i],1);
56         }
57         long long tmp;
58         long long ans=0;
59         for(int i=1;i<=n;i++){
60             add(a[i],-1);
61             for(int j=i+1;j<=n;j++){
62                 add(a[j],-1);
63                 tmp=a[i]+a[j];
64                 ans=max(ans,tmp^query(tmp));
65                 add(a[j],1);
66             }
67             add(a[i],1);
68         }
69         printf("%lld\n",ans);
70     }
71 
72 }
View Code

 

 

posted on 2018-09-30 20:04  Fighting_sh  阅读(302)  评论(0编辑  收藏  举报

导航