Restoring a Permutation
Time limit2sMemory limit1024 MB
Given a transformed permutation, construct a Robin permutation of 1 to 2n that maps to it, or report -1 if none exists.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Implementation
- Solved
- No attempts yet
Statement
Today Christopher studied sequences and permutations at school. A permutation of the numbers 1 to is a sequence in which each of these numbers appears exactly once.
Christopher especially liked these definitions:
- A descent at position in a permutation is a case where .
- A fixed point at position in a permutation is an element such that .
Christopher made up his own permutation and called it a Robin permutation. A permutation of the numbers 1 to is a Robin permutation if both of the following hold:
- has exactly descents, and all of them are at odd positions. That is, for every .
- has exactly fixed points.
For example, is a Robin permutation.
Christopher shared his discovery with Rabbit. After learning about Robin permutations, Rabbit came up with this transformation: remove all fixed points from the sequence, then replace each remaining number with the count of numbers in the remaining sequence that are not greater than it. For example, .
Christopher now wants to take a transformed permutation and find a Robin permutation that transforms into it.
Input
The first line contains , the number of elements in the transformed permutation (). The second line contains natural numbers, the transformed permutation.
Output
If no solution exists, print on the first line. If a Robin permutation exists, print one. If there are several, print any Robin permutation.