-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyet-another-minimax-problem.cpp
47 lines (38 loc) · 1.17 KB
/
yet-another-minimax-problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
int main() {
int num_integers;
cin >> num_integers;
vector<long long> integers(num_integers);
for (int i = 0; i < num_integers; i++)
cin >> integers[i];
long long min_xor = 0;
for (int bit_position = 30; bit_position >= 0; bit_position--) {
vector<int> counts(2);
for (int j = 0; j < num_integers; j++) {
counts[integers[j] >> bit_position & 1]++;
}
if (counts[0] == num_integers || counts[1] == num_integers) continue;
vector<long long> high_bit, low_bit;
for (int j = 0; j < num_integers; j++) {
if (integers[j] >> bit_position & 1) {
high_bit.push_back(integers[j]);
} else {
low_bit.push_back(integers[j]);
}
}
long long min_xor_value = LLONG_MAX;
for (long long x : high_bit) {
for (long long y : low_bit) {
min_xor_value = min(min_xor_value, x ^ y);
}
}
min_xor = min_xor_value;
break;
}
cout << min_xor << endl;
return 0;
}