Skip to main content
Back to problems
Codeforces
Hard
Arrays
Sorting
Combinatorics
Anton and Permutation

Transform a permutation so the first kk positions become the lexicographically smallest possible sequence after at most one swap per position.

Acceptance 0%
Problem Statement

Anton and Permutation

You are given a permutation of integers from $1toton$. You may perform a sequence of swaps, but each position can be involved in at most one swap.

Your task is to make the permutation as small as possible in lexicographic order under this constraint.

A lexicographically smaller permutation is one that differs at the first position where the two permutations are different, and has the smaller value there.

Idea of the task

Process the permutation from left to right and use swaps only when they improve the prefix as much as possible, while respecting the limit that every index can be used at most once.

This is a classic greedy permutation-rearrangement problem.

Input Format

  • The first line contains an integer nn.
  • The second line contains nn distinct integers describing a permutation of $1..n$.

Output Format

  • Print the lexicographically smallest permutation you can obtain under the stated swapping rule.

Constraints

  • 1n1 \le n
  • The array is a permutation of $1..n$
  • Each position may participate in at most one swap
Examples
Sample cases returned by the problem API.

Example 1

Input

5
5 4 3 2 1

Output

1 2 3 4 5

Explanation

Swapping elements greedily from the left lets the smallest values move to the front while using each position at most once.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.