atcoder#ARC080C. [ARC080E] Young Maids
[ARC080E] Young Maids
Score : points
Problem Statement
Let be a positive even number.
We have a permutation of , . Snuke is constructing another permutation of , , following the procedure below.
First, let be an empty sequence. Then, perform the following operation until becomes empty:
- Select two adjacent elements in , and call them and in order. Remove and from (reducing the length of by ), and insert and , preserving the original order, at the beginning of .
When becomes empty, will be a permutation of .
Find the lexicographically smallest permutation that can be obtained as .
Constraints
- is an even number.
- is a permutation of .
Input
Input is given from Standard Input in the following format:
Output
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:
↓ | |
↓ | |
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:
↓ | |
↓ | |
↓ | |
↓ | |