This page is still under construction.

Parts of this page are still being built. What you see may change.

Coding of Permutations

Time limit1sMemory limit128 MB

Summary
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 A=(a1,…,an)A = (a_1, \dots, a_n) of the numbers 1,…,n1, \dots, n can be encoded by a sequence B=(b1,…,bn)B = (b_1, \dots, b_n), where bib_i is the number of indices jj with j<ij < i and aj>aia_j > a_i, for i=1,…,ni = 1, \dots, n.

For example, the sequence B=(0,0,1,0,2,0,4)B = (0, 0, 1, 0, 2, 0, 4) is the code of the permutation A=(1,5,2,6,4,7,3)A = (1, 5, 2, 6, 4, 7, 3).

Write a program that:

  • reads the length nn and the successive elements of the sequence BB from standard input,
  • decides whether BB is the code of some permutation of the numbers 1,…,n1, \dots, n,
  • 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 n≤30000n \le 30000, the number of elements of the sequence BB.
  • Each of the following nn lines contains one nonnegative integer not greater than 3000030000, giving the elements of the sequence BB in order.

Output

Write the following to standard output:

  • one element of the permutation AA per line, over nn consecutive lines, where AA is the permutation whose code is the sequence BB from the input,
  • or the single word NIE if BB is not the code of any permutation.

Examples2

  1. Example 1

    Input
    7
    0
    0
    1
    0
    2
    0
    4
    
    Expected output
    1
    5
    2
    6
    4
    7
    3
    
  2. Example 2

    Input
    4
    0
    2
    0
    0
    
    Expected output
    NIE