|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +class Node { |
| 51 | + int count; |
| 52 | + Node left, right; |
| 53 | + Node(){ count = 0; } |
| 54 | +} |
| 55 | + |
| 56 | +class Trie { |
| 57 | + Node root; |
| 58 | + Trie(){ root = new Node(); } |
| 59 | + |
| 60 | + void insert(int x) { |
| 61 | + Node now = root; |
| 62 | + int bit = 1<<29; |
| 63 | + while(bit > 0) { |
| 64 | + int val = x & bit; |
| 65 | + if(val == 0) { |
| 66 | + if(now.left == null) now.left = new Node(); |
| 67 | + now = now.left; |
| 68 | + } |
| 69 | + else { |
| 70 | + if(now.right == null) now.right = new Node(); |
| 71 | + now = now.right; |
| 72 | + } |
| 73 | + now.count++; |
| 74 | + bit >>= 1; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + void erase(int x) { |
| 79 | + Node now = root; |
| 80 | + eraseDfs(now, x, 1<<29); |
| 81 | + } |
| 82 | + |
| 83 | + boolean eraseDfs(Node now, int x, int bit) { |
| 84 | + if(bit == 0) return --now.count == 0; |
| 85 | + int val = x & bit; |
| 86 | + if(val == 0) { |
| 87 | + if(eraseDfs(now.left, x, bit>>1)) now.left = null; |
| 88 | + } |
| 89 | + else { |
| 90 | + if(eraseDfs(now.right, x, bit>>1)) now.right = null; |
| 91 | + } |
| 92 | + return --now.count == 0; |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +public class Main { |
| 98 | + |
| 99 | + static IOController io; |
| 100 | + |
| 101 | + // |
| 102 | + |
| 103 | + static int N; |
| 104 | + static int[] A; |
| 105 | + static Trie P, Q; |
| 106 | + |
| 107 | + public static void main(String[] args) throws Exception { |
| 108 | + |
| 109 | + io = new IOController(); |
| 110 | + |
| 111 | + init(); |
| 112 | + solve(); |
| 113 | + |
| 114 | + io.close(); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + public static void init() throws Exception { |
| 119 | + |
| 120 | + N = io.nextInt(); |
| 121 | + A = new int[N]; |
| 122 | + for(int i=0;i<N;i++) A[i] = io.nextInt(); |
| 123 | + P = new Trie(); |
| 124 | + Q = new Trie(); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + static void solve() throws Exception { |
| 129 | + |
| 130 | + P.insert(A[0]); |
| 131 | + for(int i=1;i<N;i++) Q.insert(A[i]); |
| 132 | + |
| 133 | + long mst = 0; |
| 134 | + for(int i=1;i<N;i++) mst += set(); |
| 135 | + |
| 136 | + io.write(mst + "\n"); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + static int set() { |
| 141 | + int[] result = find(P.root, Q.root, 1<<29); |
| 142 | + int cost = result[0], val = result[1]; |
| 143 | + Q.erase(val); |
| 144 | + P.insert(val); |
| 145 | + return cost; |
| 146 | + } |
| 147 | + |
| 148 | + static int[] find(Node p, Node q, int bit) { |
| 149 | + |
| 150 | + if(bit == 0) return new int[]{0,0}; |
| 151 | + |
| 152 | + boolean c1 = p.left != null && q.left != null; |
| 153 | + boolean c2 = p.right != null && q.right != null; |
| 154 | + boolean c3 = p.left != null && q.right != null; |
| 155 | + boolean c4 = p.right != null && q.left != null; |
| 156 | + |
| 157 | + if(c1 || c2) { |
| 158 | + if(c1 && c2) { |
| 159 | + int[] res1 = find(p.left, q.left, bit>>1); |
| 160 | + int[] res2 = find(p.right, q.right, bit>>1); |
| 161 | + res2[1] |= bit; |
| 162 | + if(res1[0] > res2[0]) return res2; |
| 163 | + return res1; |
| 164 | + } |
| 165 | + if(c1) return find(p.left, q.left, bit>>1); |
| 166 | + int[] res = find(p.right, q.right, bit>>1); |
| 167 | + res[1] |= bit; |
| 168 | + return res; |
| 169 | + } |
| 170 | + if(c3 && c4) { |
| 171 | + int[] res1 = find(p.left, q.right, bit>>1); |
| 172 | + res1[0] |= bit; |
| 173 | + res1[1] |= bit; |
| 174 | + int[] res2 = find(p.right, q.left, bit>>1); |
| 175 | + res2[0] |= bit; |
| 176 | + if(res1[0] > res2[0]) return res2; |
| 177 | + return res1; |
| 178 | + } |
| 179 | + if(c3) { |
| 180 | + int[] res = find(p.left, q.right, bit>>1); |
| 181 | + res[0] |= bit; |
| 182 | + res[1] |= bit; |
| 183 | + return res; |
| 184 | + } |
| 185 | + int[] res = find(p.right, q.left, bit>>1); |
| 186 | + res[0] |= bit; |
| 187 | + return res; |
| 188 | + |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | +``` |
0 commit comments