#include "two-sort.h" #include #include #include #include #include class ArraySorter { public: ArraySorter(const std::vector& a, const std::vector& b) : a(a), b(b) {} void apply_move(int x) { if (x < 1 || x >= static_cast(a.size() + b.size()) + 1){ throw std::invalid_argument("x is out of bounds"); } auto& from = (std::find(a.cbegin(), a.cend(), x) != a.cend()) ? a : b; auto& to = (&from == &a) ? b : a; from.erase(std::remove(from.begin(), from.end(), x), from.end()); to.push_back(x); } bool is_sorted() const { return std::is_sorted(a.begin(), a.end()) && std::is_sorted(b.begin(), b.end()); } private: std::vector a, b; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int num_tests; assert(std::cin >> num_tests); for (int t = 0; t < num_tests; t++) { int n, m; assert(std::cin >> n >> m); std::vector a(n), b(m); for (auto& x : a) { assert(std::cin >> x); } for (auto& x : b) { assert(std::cin >> x); } const auto contest_solution = solve(n, m, a, b); std::cout << contest_solution.size() << "\n"; for (size_t i{}; i < contest_solution.size(); i++) { if (i > 0u) { std::cout << " "; } std::cout << contest_solution[i]; } std::cout << "\n"; ArraySorter sorter(a, b); for (const auto& x : contest_solution) { sorter.apply_move(x); } if (!sorter.is_sorted()) { std::cerr << "Moves do not sort the arrays\n"; return 1; } } return 0; }