

Time Limit: 2 sec / Memory Limit: 256 MB
配点 : 点
問題文
を正の偶数とします。
の順列 があります。 すぬけ君は、次の手続きによって の順列 を作ろうとしています。
まず、空の数列 を用意します。 が空になるまで、次の操作を繰り返します。
- の隣り合う つの要素を選び、順に , とする。 , を から取り除き (このとき、 は だけ短くなる)、, をこの順のまま の先頭へ追加する。
が空になったとき、 は の順列になっています。
辞書順で最小の を求めてください。
制約
- は偶数である。
- は の順列である。
入力
入力は以下の形式で標準入力から与えられる。
出力
辞書順で最小の を空白区切りで出力せよ。
入力例 1Copy
4 3 2 4 1
出力例 1Copy
3 1 2 4
次の順に操作を行えばよいです。
↓ | ↓ |
↓ | ↓ |
入力例 2Copy
2 1 2
出力例 2Copy
1 2
入力例 3Copy
8 4 6 3 2 8 5 7 1
出力例 3Copy
3 1 2 7 4 6 8 5
次の順に操作を行えばよいです。
↓ | ↓ |
↓ | ↓ |
↓ | ↓ |
↓ | ↓ |
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.
Sample Input 1Copy
4 3 2 4 1
Sample Output 1Copy
3 1 2 4
The solution above is obtained as follows:
↓ | ↓ |
↓ | ↓ |
Sample Input 2Copy
2 1 2
Sample Output 2Copy
1 2
Sample Input 3Copy
8 4 6 3 2 8 5 7 1
Sample Output 3Copy
3 1 2 7 4 6 8 5
The solution above is obtained as follows:
↓ | ↓ |
↓ | ↓ |
↓ | ↓ |
↓ | ↓ |