// O(N^(3/2)) solution. #include #include #include #include #include #include using namespace std; class Picker { public: Picker(int N) : max_holes(2 * sqrt(N)) { for (int i = 0; i < N; ++i) { remaining.push_back(i + 1); } } // ith is 1-based int pick(int ith) { ith--; int i; for (i = 0; i < indices_picked.size(); ++i) { if (indices_picked[i] <= ith) { ith++; } else { break; } } indices_picked.insert(indices_picked.begin() + i, ith); const int chosen = remaining[ith]; if (indices_picked.size() > max_holes) { remove_holes(); } return chosen; } private: void remove_holes() { vector new_remaining; int j = 0; for (int i = 0; i < remaining.size(); ++i) { while (j < indices_picked.size() && indices_picked[j] < i) j++; if (j >= indices_picked.size() || indices_picked[j] > i) { new_remaining.push_back(remaining[i]); } } remaining = std::move(new_remaining); indices_picked.clear(); } vector indices_picked; vector remaining; const int max_holes; }; std::array, 2> pick_players(const std::array, 2> &choices) { Picker picker(choices[0].size() * 2); std::array, 2> teams; for (int i = 0; i < choices[0].size(); ++i) { for (int t = 0; t <= 1; ++t) { teams[t].push_back(picker.pick(choices[t][i])); } } return teams; } int main() { int N; cin >> N; assert(N % 2 == 0); std::array, 2> choices; choices[0].reserve(N); choices[1].reserve(N); for (int i = 0; i < N; ++i) { int choice; cin >> choice; // In the problem statement, the indexes are 1-based. choices[i >= N / 2].push_back(choice); } auto teams = pick_players(choices); for (const auto &team : teams) { for (int player : team) { cout << player << " "; } cout << endl; } }