Download 2021/finals/and.cpp from hackercupai/hackercup: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/datasets/hackercupai/hackercup/resolve/7f81acc577ea2c9978ee47b7df386f783ca8cb4d/2021/finals/and.cpp
- Command line
-
hf download hf://datasets/hackercupai/hackercup@7f81acc577ea2c9978ee47b7df386f783ca8cb4d/2021/finals/and.cpp
-
curl -L -o and.cpp https://huggingface.co/datasets/hackercupai/hackercup/resolve/7f81acc577ea2c9978ee47b7df386f783ca8cb4d/2021/finals/and.cpp
3.12 kB
| using namespace std; | |
| const int LIM = 4000005; | |
| struct UnionFind { | |
| int N; | |
| vector<int> root, diff_to_root, rank, sz, diffsum; | |
| UnionFind(int _N): | |
| N(_N), root(_N), diff_to_root(_N), rank(_N), sz(_N, 1), diffsum(_N) { | |
| for (int i = 0; i < N; i++) { | |
| root[i] = i; | |
| } | |
| } | |
| int find(int i) { | |
| int r = root[i]; | |
| if (r != i) { | |
| root[i] = find(r); | |
| diff_to_root[i] ^= diff_to_root[r]; | |
| } | |
| return root[i]; | |
| } | |
| bool merge(int i, int j, int d) { | |
| int ri = find(i), rj = find(j); | |
| d ^= diff_to_root[i] ^ diff_to_root[j]; | |
| if (ri == rj) { | |
| return !d; | |
| } | |
| if (rank[ri] > rank[rj]) { | |
| swap(ri, rj); | |
| } | |
| root[ri] = rj; | |
| diff_to_root[ri] = d; | |
| sz[rj] += sz[ri]; | |
| diffsum[rj] += d ? sz[ri] - diffsum[ri] : diffsum[ri]; | |
| if (rank[ri] == rank[rj]) { | |
| rank[rj]++; | |
| } | |
| return true; | |
| } | |
| }; | |
| int N, K; | |
| void solve() { | |
| vector<int> both(LIM), ans(LIM); | |
| vector<vector<vector<int>>> ind(LIM, vector<vector<int>>(2)); | |
| int maxL = 0; | |
| // Input. | |
| cin >> N >> K; | |
| for (int i = 0; i < N; i++) { | |
| string s[2]; | |
| cin >> s[0] >> s[1]; | |
| int len = max(s[0].size(), s[1].size()); | |
| maxL = max(maxL, len); | |
| for (int b = 0; b < len; b++) { | |
| int ch[2]; | |
| for (int j : {0, 1}) { | |
| int slen = s[j].size(); | |
| ch[j] = b < slen ? s[j][slen - b - 1] - '0' : 0; | |
| } | |
| if (ch[0] && ch[1]) { | |
| both[b]++; | |
| } else { | |
| for (int j : {0, 1}) { | |
| if (ch[j]) { | |
| ind[b][j].push_back(i); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // Consider each bit, starting from the most significant. | |
| UnionFind U(N); | |
| for (int b = maxL - 1; b >= 0; b--) { | |
| ans[b] = 0; | |
| // Bit included twice? | |
| if (both[b] == N) { | |
| ans[b] = 2; | |
| continue; | |
| } | |
| // Bit not included at all? | |
| if (both[b] + ind[b][0].size() + ind[b][1].size() < N) { | |
| continue; | |
| } | |
| // Include bit once if possible. | |
| int k = 0; | |
| bool imp = false; | |
| UnionFind U2 = U; | |
| for (int i = 0; !imp && i < 2; i++) { | |
| for (int j = 0; !imp && j < (int)ind[b][i].size() - 1; j++) { | |
| if (!U2.merge(ind[b][i][j], ind[b][i][j + 1], 0)) { | |
| imp = true; | |
| } | |
| } | |
| } | |
| if (imp) { | |
| continue; | |
| } | |
| if (ind[b][0].size() > 0 && ind[b][1].size() > 0) { | |
| if (!U2.merge(ind[b][0][0], ind[b][1][0], 1)) { | |
| continue; | |
| } | |
| } | |
| for (int i = 0; i < N; i++) { | |
| if (U2.find(i) == i) { | |
| k += min(U2.diffsum[i], U2.sz[i] - U2.diffsum[i]); | |
| } | |
| } | |
| if (k > K) { | |
| continue; | |
| } | |
| U = U2; | |
| ans[b] = 1; | |
| } | |
| // Add up answer in binary. | |
| for (int i = 0; i < maxL; i++) { | |
| if (i == maxL - 1 && ans[i] >= 2) { | |
| ans[maxL++] = 0; | |
| } | |
| ans[i + 1] += ans[i] / 2; | |
| ans[i] %= 2; | |
| } | |
| // Output. | |
| while (maxL > 1 && !ans[maxL - 1]) { | |
| maxL--; | |
| } | |
| for (int i = maxL - 1; i >= 0; i--) { | |
| cout << ans[i]; | |
| } | |
| cout << endl; | |
| } | |
| int main() { | |
| int T; | |
| cin >> T; | |
| for (int t = 1; t <= T; t++) { | |
| cout << "Case #" << t << ": "; | |
| solve(); | |
| } | |
| return 0; | |
| } | |