HDU 5536 Chip Factory 【01字典树删除】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536

Chip Factory

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

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
 

题目大意:

给 N 个数,在这 N 个数里找到三个值 i, j,k 使得(i+j)⊕ k 最大,输出这个最大值。

解题思路:

每次在01字典树里删除 i 和 j 然后 (i+j)和01字典树里的匹配求最大异或值。

AC code:

 1 #include <bits/stdc++.h>
 2 #define ll long long int
 3 #define INF 0x3f3f3f3f
 4 using namespace std;
 5 
 6 const int MAXN = 1e3+10;
 7 int ch[MAXN*32][2];
 8 ll value[MAXN*32];
 9 ll b[MAXN];
10 int num[MAXN*32];
11 int node_cnt;
12 
13 void init()
14 {
15     memset(ch[0], 0, sizeof(ch[0]));
16     node_cnt = 1;
17 }
18 
19 void Insert(ll x)
20 {
21     int cur = 0;
22     for(int i = 32; i >= 0; i--){
23         int index = (x>>i)&1;
24         if(!ch[cur][index]){
25             memset(ch[node_cnt], 0, sizeof(ch[node_cnt]));
26             ch[cur][index] = node_cnt;
27             value[node_cnt] = 0;
28             num[node_cnt++] = 0;
29         }
30         cur = ch[cur][index];
31         num[cur]++;
32     }
33     value[cur] = x;
34 }
35 
36 void update(ll x, int d)
37 {
38     int cur = 0;
39     for(int i =32; i >= 0; i--){
40         int index = (x>>i)&1;
41         cur = ch[cur][index];
42         num[cur]+=d;
43     }
44 }
45 
46 ll query(ll x)
47 {
48     int cur = 0;
49     for(int i = 32; i >= 0; i--)
50     {
51         int index = (x>>i)&1;
52         if(ch[cur][index^1] && num[ch[cur][index^1]]) cur = ch[cur][index^1];
53         else cur = ch[cur][index];
54     }
55     return x^value[cur];
56 }
57 
58 int main()
59 {
60     int T_case, N;
61     scanf("%d", &T_case);
62     while(T_case--)
63     {
64         scanf("%d", &N);
65         init();
66         for(int i = 1; i <= N; i++)
67         {
68             scanf("%lld", &b[i]);
69             Insert(b[i]);
70         }
71         ll ans = 0;
72         for(int i = 1; i <= N; i++){
73             for(int j = 1; j <= N; j++){
74                 if(i == j) continue;
75                 update(b[i], -1);
76                 update(b[j], -1);
77                 ans = max(ans, query(b[i]+b[j]));
78                 update(b[i], 1);
79                 update(b[j], 1);
80             }
81         }
82         printf("%lld\n", ans);
83     }
84     return 0;
85 }
View Code

猜你喜欢

转载自www.cnblogs.com/ymzjj/p/9544467.html
今日推荐