Submission #914327

# Submission time Handle Problem Language Result Execution time Memory
914327 2024-01-21T16:11:18 Z NK_ MalnaRISC (COI21_malnarisc) C++17
100 / 100
1 ms 600 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using vpi = V<pi>;

mt19937 rng;

const int MX = 40;
int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; cin >> N;
	vi A(N); iota(begin(A), end(A), 0);
	shuffle(begin(A), end(A), rng);

	// for(auto& x : A) cout << x << " ";
	// cout << endl;

	V<vpi> ans(MX);

	int L = 0;
	auto swp = [&](int x, int y) {
		if (max(x, y) >= N) return;
		if (A[x] > A[y]) swap(A[x], A[y]);
		// cout << x << " <-> " << y << endl;
		ans[L].pb(mp(x, y));
	};

	int n = 1, lg = 0; while(n < N) n *= 2, lg++;

	for(int i = 1; i <= lg; i++) {
		V<vpi> swps(i);
		// cout << "ITER: " << i << endl;
		for(int l = 0, r = (1 << i) - 1; r < n; l += (1 << i), r += (1 << i)) {
			// cout << "RANGE: " << l << " " << r << endl;
			int len = 1 << (i - 1);
			int m = (l + r) / 2;
			for(int x = l; x <= m; x++) {
				swps[0].pb(mp(x, x + len));
			}

			int line = 1;
			for(int b = i - 2; b >= 0; b--, line++) {
				for(int x = l; x <= r; x++) {
					if (((x - l) >> b) & 1) {
						int y = x + (1 << b); if (y > r) continue;
						swps[line].pb(mp(x, x + (1 << b)));
					}
				}
			}
		}

		for(int k = 0; k < i; k++) {
			if (!sz(swps[k])) continue;
			// cout << "LINE: " << k << endl;
			for(auto& p : swps[k]) swp(p.f, p.s);
			L++;
		}

		// cout << endl;
	}

	cout << L << nl;
	for(int x = 0; x < L; x++) {
		for(auto& p : ans[x]) {
			cout << "CMPSWP R" << p.f+1 << " R" << p.s+1 << " ";
		}
		cout << nl;
	}
			
	// cout << endl << endl;
	// for(auto& x : A) cout << x << " ";
	// cout << endl;


	exit(0-0);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct