This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
https://en.wikipedia.org/wiki/Batcher_odd%E2%80%93even_mergesort
*/
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) static_cast<int>(v.size())
#define all(v) (v).begin(), (v).end()
typedef pair<int, int> pi;
typedef long long ll;
typedef vector<int> vi;
#if 0
void solve(int n, int v);
void schedule(int i, int j);
vi visit();
void answer(vi v);
#else
#include "swaps.h"
#endif
void solve(int n, int v) {
vi p(n);
iota(all(p), 1);
vector<pair<int, int>> t;
auto sch = [&](int i, int j) {
schedule(p[i], p[j]);
t.push_back({i, j});
};
auto vis = [&]() {
if (sz(t)) {
vi v = visit();
for (int h = 0; h < sz(t); h++)
if (!v[h]) {
auto [i, j] = t[h];
swap(p[i], p[j]);
}
t.clear();
}
};
for (int p = 1; p < n; p *= 2)
for (int k = p; k >= 1; k /= 2) {
for (int j = k % p; j < n - k; j += k * 2)
for (int i = 0; i < min(k, n - j - k); i++)
if ((i + j) / (p * 2) == (i + j + k) / (p * 2)) sch(i + j, i + j + k);
vis();
}
answer(p);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |