// batcher's odd-even merge sort
// https://math.mit.edu/~shor/18.310/batcher.pdf
#include "swaps.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
using namespace std;
namespace{
const int maxn = 512;
}
void solve(int n, int v) {
vector<pair<int, int>> ops;
vector<int> vec(n);
for(int i = 0; i < n; i++) vec[i] = i + 1;
auto add = [&](int a, int b){
if(a >= n || b >= n) return;
schedule(vec[a], vec[b]);
ops.emplace_back(a, b);
return;
};
auto do_ops = [&](){
auto res = visit();
int sz = (int)ops.size();
for(int i = 0; i < sz; i++){
auto &[a, b] = ops[i];
if(res[i] == 0) swap(vec[a], vec[b]);
// whether or not the grader swaps paintings actually doesn't matter!
// calling comp(a, b) means that we don't know their order yet and whether a or b is bigger doesn't mess up the order
// in the case of batcher's odd-even merge sort the grouped pairs are already in order
}
vector<pair<int, int>>().swap(ops);
return;
};
vector<vector<vector<pair<int, int>>>> srtd(10);
srtd[1].push_back({make_pair(0, 1)});
for(int i = 0; i < maxn; i += 2){
add(i, i + 1);
}
do_ops();
for(int i = 2; i < 10; i++){
// 0, 2, 4, ..., (1 << i) - 2
// 1, 3, 5, ..., (1 << i) - 1
vector<int> odd, even;
for(int j = 0; j < (1 << i); j += 2) even.push_back(j), odd.push_back(j + 1);
int sz = (int)srtd[i - 1].size();
for(int j = 0; j < sz; j++){
vector<pair<int, int>> now;
for(auto &[a, b]: srtd[i - 1][j]){
now.emplace_back(odd[a], odd[b]);
now.emplace_back(even[a], even[b]);
}
srtd[i].push_back(now);
}
vector<pair<int, int>> lst_op;
for(int j = 1; j < (1 << i) - 1; j += 2){
lst_op.emplace_back(j, j + 1);
}
srtd[i].push_back(lst_op);
for(auto &now: srtd[i]){
for(auto [a, b]: now){
while(a < maxn && b < maxn){
add(a, b);
a += (1 << i);
b += (1 << i);
}
}
do_ops();
}
}
answer(vec);
}
// g++ -std=c++20 -Wall -Wextra -Wshadow -fsanitize=undefined -fsanitize=address -o run swaps.cpp grader.cpp
# | 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... |