Score : $800$ points
Let $N$ be a positive even number.
We have a permutation of $(1, 2, ..., N)$, $p = (p_1, p_2, ..., p_N)$. Snuke is constructing another permutation of $(1, 2, ..., N)$, $q$, following the procedure below.
First, let $q$ be an empty sequence. Then, perform the following operation until $p$ becomes empty:
When $p$ becomes empty, $q$ will be a permutation of $(1, 2, ..., N)$.
Find the lexicographically smallest permutation that can be obtained as $q$.
Input is given from Standard Input in the following format:
$N$ $p_1$ $p_2$ $...$ $p_N$
Print the lexicographically smallest permutation, with spaces in between.
4 3 2 4 1
3 1 2 4
The solution above is obtained as follows:
$p$ | $q$ |
---|---|
$(3, 2, 4, 1)$ | $()$ |
↓ | ↓ |
$(3, 1)$ | $(2, 4)$ |
↓ | ↓ |
$()$ | $(3, 1, 2, 4)$ |
2 1 2
1 2
8 4 6 3 2 8 5 7 1
3 1 2 7 4 6 8 5
The solution above is obtained as follows:
$p$ | $q$ |
---|---|
$(4, 6, 3, 2, 8, 5, 7, 1)$ | $()$ |
↓ | ↓ |
$(4, 6, 3, 2, 7, 1)$ | $(8, 5)$ |
↓ | ↓ |
$(3, 2, 7, 1)$ | $(4, 6, 8, 5)$ |
↓ | ↓ |
$(3, 1)$ | $(2, 7, 4, 6, 8, 5)$ |
↓ | ↓ |
$()$ | $(3, 1, 2, 7, 4, 6, 8, 5)$ |