#GD0000210. Tối thiểu hóa hoán vị (Minimize the Permutation)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tối thiểu hóa hoán vị (Minimize the Permutation)
Minimize the Permutation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of the integers through . For each from to , operation swaps the elements currently at adjacent positions and .
Operations may be performed in any order, but each operation may be used at most once. Performing no operations is also allowed.
Find the lexicographically smallest permutation obtainable. Compare permutations from left to right; the smaller element at the first differing position determines the smaller permutation.
Input
- The first line contains an integer , the permutation length.
- The second line contains exactly integers in their initial order.
Output
Print one line of exactly space-separated integers: the lexicographically smallest permutation attainable when every adjacent swap position can be used at most once.
Subtasks
- Subtask 1 (20 points): ; is a permutation of with distinct values.
- Subtask 2 (30 points): ; is a permutation of with distinct values.
- Subtask 3 (50 points): ; is a permutation of with distinct values.
Examples
Input
5
5 4 1 3 2
Output
1 5 2 4 3
Explanation
From 5 4 1 3 2, use swap position 2 to obtain 5 1 4 3 2; position 4 gives 5 1 4 2 3; position 3 gives 5 1 2 4 3; and position 1 gives 1 5 2 4 3. Every swap position is used once. This is the lexicographically minimum attainable permutation.