[Code Jam 2022 Round 1A] Equal Sum

Equal Sum

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
#include <bits/stdc++.h>
using namespace std;

vector<int> solve(int n) {
vector<int> A, B;

vector<int> pows{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912};
vector<int> sorted{3,5,6,7,9,10,11,12,13,14,15,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,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,65,66,67,68,69,70,71,72,73,74,75,76,77};
int diff = 0, num;
for(auto& p : pows) cout<<p<<" ";
for(auto& st : sorted) cout<<st<<" ";cout<<endl;

for(int i = 0; i < n; i++) {
cin>>num;
sorted.push_back(num);
}

sort(begin(sorted), end(sorted));

for(int i = sorted.size() - 1; i >= 0; i--) {
if(diff >= 0) {
B.push_back(sorted[i]);
diff -= sorted[i];
} else {
A.push_back(sorted[i]);
diff += sorted[i];
}
}

for(int i = pows.size() - 1; i >= 0; i--) {
if(diff >= 0) {
B.push_back(pows[i]);
diff -= pows[i];
} else {
A.push_back(pows[i]);
diff += pows[i];
}
}

return A;
}

int main() {
int tc;
cin>>tc;
for(int i = 1; i <= tc; i++) {
int n;
cin>>n;
//cout<<"Case #"<<i<<": "<<solve(s)<<endl;
auto res = solve(n);
for(auto& n : res) cout<<n<<" "; cout<<endl;
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/09/PS/Google/equal-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.