|
1 |
| -#include <bits/stdc++.h> |
2 |
| -typedef long long ll; |
3 |
| -using namespace std; |
| 1 | +/* |
| 2 | +COI 2021 MalnaRISC |
| 3 | +- This is a nerfed version of Baltic '21 Swaps, so see that solution too |
| 4 | +- https://en.wikipedia.org/wiki/Bitonic_sorter |
| 5 | +- https://en.wikipedia.org/wiki/Batcher_odd%E2%80%93even_mergesort |
| 6 | +- O(log^2 N) steps to sort in parallel |
| 7 | +*/ |
4 | 8 |
|
5 |
| -vector<pair<int, int>> swaps[5000]; |
6 |
| - |
7 |
| -int swapsort(int l, int r) { |
8 |
| - if (l == r) return 0; |
9 |
| - |
10 |
| - int mid = (l + r) / 2; |
11 |
| - int depth = max(swapsort(l, mid), swapsort(mid + 1, r)); |
12 |
| - |
13 |
| - for (int i = 0; i <= mid - l; i++) { |
14 |
| - for (int x = mid, y = r + (mid - l != r - mid - 1) - i; x >= l + i; x--, y--) |
15 |
| - if (y <= r) swaps[depth + 1 + i].push_back({x, y}); |
16 |
| - } |
17 |
| - return depth + mid - l + 1; |
18 |
| -} |
| 9 | +#include <stdio.h> |
19 | 10 |
|
20 | 11 | int main() {
|
21 |
| - cin.tie(0)->sync_with_stdio(0); |
22 | 12 | int n;
|
23 |
| - cin >> n; |
24 |
| - int depth = swapsort(1, n); |
25 |
| - cout << depth << '\n'; |
26 |
| - for (int i = 1; i <= depth; i++) { |
27 |
| - for (pair<int, int> j : swaps[i]) if (j.second <= n) |
28 |
| - cout << "CMPSWP R" << j.first << " R" << j.second << ' '; |
29 |
| - cout << '\n'; |
30 |
| - } |
| 13 | + scanf("%d", &n); |
| 14 | + int p2 = 1, blocks = 0; |
| 15 | + while (p2 < n) p2 <<= 1, blocks++; |
| 16 | + printf("%d\n", blocks * (blocks + 1) / 2); |
| 17 | + for (int p = 1; p < p2; p <<= 1) |
| 18 | + for (int k = p; k; k >>= 1) { |
| 19 | + for (int j = k % p; j < p2 - k; j += 2 * k) |
| 20 | + for (int i = 0; i < k && i + j + k < n; i++) |
| 21 | + if ((i + j) / (2 * p) == (i + j + k) / (2 * p)) |
| 22 | + printf("CMPSWP R%d R%d ", i + j + 1, i + j + k + 1); |
| 23 | + printf("\n"); |
| 24 | + } |
31 | 25 | return 0;
|
32 | 26 | }
|
0 commit comments