-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf-1017.cpp
More file actions
89 lines (77 loc) · 1.84 KB
/
cf-1017.cpp
File metadata and controls
89 lines (77 loc) · 1.84 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
ll x;
cin >> x;
vector<ll> v(x);
for (int i = 0; i < x; i++) {
cin >> v[i];
}
// Store 32-bit binary strings
vector<string> store;
for (int i = 0; i < x; i++) {
string s = "";
for (int j = 31; j >= 0; j--) {
s += ((v[i] >> j) & 1) ? '1' : '0';
}
store.push_back(s);
}
vector<bool> eliminated(x, false);
int remaining = x;
for (int i = 31; i >=0; i--) {
int zeroCount = 0, oneCount = 0;
for (int j = 0; j < x; j++) {
if (store[j][i] == '0') zeroCount++;
else oneCount++;
}
char majority = (zeroCount > oneCount) ? '0' : '1';
int flg=0;
for(int j=0;j<x;j++){
if(!eliminated[j] && store[j][i]!=majority){
flg=1;
}
}
int count=0,count1=0;
for(int j=0;j<x;j++){
if(!eliminated[j] && store[j][i]==majority){
count++;
}
}
for(int j=0;j<x;j++){
if(eliminated[j]){
count1++;
}
}
cout<<count+count1<<" ";
if(flg==0 || (count1+count==x)) continue;
for (int j = 0; j < x; j++) {
if (eliminated[j]) continue;
if (store[j][i] == majority) {
eliminated[j] = true;
remaining--;
}
}
}
ll val = -1;
for (int i = 0; i < x; i++) {
if (!eliminated[i]) {
val = v[i];
break;
}
}
cout << val << " ";
ll ans = 0;
for (int i = 0; i < x; i++) {
ans += (v[i] ^ val);
}
cout << ans << endl;
}
int main() {
ll t;
cin >> t;
while (t--) {
solve();
}
return 0;
}