Coding of Permutations
Time limit1sMemory limit128 MB
Given a Lehmer-like code B, decide whether it encodes a permutation of 1..n and if so output that permutation, otherwise print NIE.
- Level
Medium6 of 10
- Topics
- Segment tree, Binary search, Implementation, Math
- Solved
- No attempts yet
Problem
Every permutation of the numbers can be encoded by a sequence , where is the number of indices with and , for .
For example, the sequence is the code of the permutation .
Write a program that:
- reads the length and the successive elements of the sequence from standard input,
- decides whether is the code of some permutation of the numbers ,
- if it is, finds that permutation and writes it to standard output,
- otherwise, writes the single word
NIE("no") to standard output.
Input
- The first line of standard input contains a positive integer , the number of elements of the sequence .
- Each of the following lines contains one nonnegative integer not greater than , giving the elements of the sequence in order.
Output
Write the following to standard output:
- one element of the permutation per line, over consecutive lines, where is the permutation whose code is the sequence from the input,
- or the single word
NIEif is not the code of any permutation.