[2019 Hang electrical multi-correction fifth] [hdu6625] three arrays

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=6625

The effect is to give you two arrays a and B, to give the corresponding position of the XOR array c, can now be a, b c array seek new sorting array, such that the lexicographically smallest.

General practice is to use two arrays of digital binary tree built two dictionaries, and record the number of each location. Then at the same time dfs two trie, 0-0 and 1-1 to the priority direction to go, can not go longer follow 0-1,1-0 direction.

0-0 and 1-1 in both cases because no particular order, so to come out is not necessarily the smallest, the end of the resulting array to be sorted c.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 const int maxn = 100000 + 5;
 9 struct Trie {
10     int tree[maxn * 30][2], num[maxn * 30];
11     int len, root;
12     int newcode() {
13         tree[len][0] = tree[len][1] = 0;
14         num[len] = 0;
15         return len++;
16     }
17     void init() {
18         len = 0;
19         root = newcode();
20     }
21     void insert(int x) {
22         int now = root;
23         for (int i = 29; i >= 0; i--) {
24             int k = (x >> i) & 1;
25             if (!tree[now][k])
26                 tree[now][k] = newcode();
27             now = tree[now][k];
28             num[now]++;
29         }
30     }
31 }A, B;
32 int ans[maxn];
33 void slove(int n) {
34     for (int i = 1; i <= n; i++) {
35         ans[i] = 0;
36         int nowA = 0, nowB = 0;
37         for (int j = 29; j >= 0; j--) {
38             if (A.num[A.tree[nowA][0]] && B.num[B.tree[nowB][0]]) {
39                 nowA = A.tree[nowA][0], nowB = B.tree[nowB][0];
40                 A.num[nowA]--, B.num[nowB]--;
41             }
42             else if (A.num[A.tree[nowA][1]] && B.num[B.tree[nowB][1]]) {
43                 nowA = A.tree[nowA][1], nowB = B.tree[nowB][1];
44                 A.num[nowA]--, B.num[nowB]--;
45             }
46             else if (A.num[A.tree[nowA][0]] && B.num[B.tree[nowB][1]]) {
47                 nowA = A.tree[nowA][0], nowB = B.tree[nowB][1];
48                 A.num[nowA]--, B.num[nowB]--;
49                 ans[i] += (1 << j);
50             }
51             else if (A.num[A.tree[nowA][1]] && B.num[B.tree[nowB][0]]) {
52                 nowA = A.tree[nowA][1], nowB = B.tree[nowB][0];
53                 A.num[nowA]--, B.num[nowB]--;
54                 ans[i] += (1 << j);
55             }
56         }
57     }
58 }
59 int main() {
60     int t;
61     scanf("%d", &t);
62     while (t--) {
63         int n, x;
64         A.init(), B.init();
65         scanf("%d", &n);
66         for (int i = 1; i <= n; i++)
67             scanf("%d", &x), A.insert(x);
68         for (int i = 1; i <= n; i++)
69             scanf("%d", &x), B.insert(x);
70         slove(n);
71         sort(ans + 1, ans + 1 + n);
72         for (int i = 1; i <= n; i++)
73             printf("%d%c", ans[i], i == n ? '\n' : ' ');
74     }
75 
76 }

 

Guess you like

Origin www.cnblogs.com/sainsist/p/11354638.html