No.2740 Old Maid
問題文最終更新日: 2024-04-19 01:07:36
English (AI translation)
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence.
Back to the original
CPCTF 2024 : PPC
(順位表)
の他の問題:
Problem Statement
Let $N$ be a positive even number.
There is a permutation $p=(p_1, p_2, \dots, p_N)$ of $(1, 2, \dots N)$. Torao is trying to construct a permutation $q$ of $(1, 2, \dots, N)$ by the following procedure.
First, prepare an empty sequence $q$. Then repeat the following operation until $p$ becomes empty.
- Choose $2$ adjacent elements of $p$, and call them $x, y$. Remove $x, y$ from $p$ (at this point, $p$ becomes shorter by $2$), and append $x, y$ to the end of $q$, keeping them in this order.
When $p$ becomes empty, $q$ is a permutation of $(1,2,...,N)$.
Find the lexicographically smallest $q$.
Constraints
- $N$ is even.
- $2 \le N \le 2\times 10^5$
- $(p_1, p_2, \dots, p_N)$ is a permutation of $(1, 2, \dots, N)$.
Input
The input is given from standard input in the following format.
$N$ $p_1$ $p_2$ $\dots$ $p_N$
Output
Output the lexicographically smallest $q$, separated by spaces.
Sample
Sample 1
Input
4 3 2 4 1
Output
2 4 3 1
The operations can be performed in the following order.
$p$ $q$ $(3, 2, 4, 1)$ $()$ $(3, 1)$ $(2, 4)$ $()$ $(2, 4, 3, 1)$
Sample 2
Input
2 1 2
Output
1 2
Sample 3
Input
8 4 6 3 2 8 5 7 1
Output
2 8 3 5 4 6 7 1
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。