Baltic OI '16 P6 - Swap

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 256M

Problem type
Baltic Olympiad in Informatics: 2016 Day 2, Problem 3

You are given a sequence of n numbers x_1, x_2, \dots, x_n. Each number 1, 2, \dots, n appears exactly once in the sequence.

You can modify the sequence using swaps. There are n-1 consecutive turns numbered k = 2, 3, \dots, n. On turn k you can either swap values x_k and x_{\lfloor k/2 \rfloor} in the sequence or do nothing.

Sequence a_1, a_2, \dots, a_n is lexicographically smaller than sequence b_1, b_2, \dots, b_n if there exists an index j (1 \le j \le n) such that a_k = b_k for all k < j and a_j < b_j.

What is the lexicographically minimal sequence you can obtain?

Constraints

Subtask 1 [10%]

1 \le n \le 20

Subtask 2 [11%]

1 \le n \le 40

Subtask 3 [27%]

1 \le n \le 1\,000

Subtask 4 [20%]

1 \le n \le 5 \cdot 10^4

Subtask 5 [32%]

1 \le n \le 2 \cdot 10^5

Input Specification

The first input line contains an integer n.

The second input line contains n integers: the numbers in the sequence.

Output Specification

You should output n integers: the lexicographically minimal sequence.

Sample Input 1

5
3 4 2 5 1

Sample Output 1

2 1 3 4 5

Comments

There are no comments at the moment.