Under construction

Still being built. Things may change

Chain

Interview

Time limit2sMemory limit512 MB

Summary
For each position, repeatedly jump to the first strictly greater element on its right and report the length of that chain.
Level

Medium6 of 10

Topics
Stack, Dynamic programming, Array
Solved
No attempts yet

Problem

Given is a sequence of NN integers a1,a2,…,ana_1, a_2, \ldots, a_n. For any element aka_k (k=1,2,…,nk = 1, 2, \ldots, n) we find the first element larger than aka_k that is placed to the right of aka_k (if such exists). Denote it by ak1a_{k1}. Then do the same with ak1a_{k1} and denote the found element by ak2a_{k2}, and so on until we run out of the given sequence. Thus formed subsequence ak1,ak2,…a_{k1}, a_{k2}, \ldots we call the chain beginning at index kk.

Write program chain that outputs for any index kk the length of the corresponding chain that begins at index kk.

Input

On the first line of the standard input, the value of NN is written. On the second line, the elements of the given sequence are written, separated by spaces.

Output

On a line in the standard output, your program has to write the sequence of chain's lengths, corresponding to the elements of the input data. Each two consecutive numbers in the output must be separated by a single space.

Constraints

  • 0<N<500 0000 < N < 500\,000
  • 0<ai<1 000 0000 < a_i < 1\,000\,000, for each i=1,…,Ni = 1, \ldots, N.

Examples1

  1. Example 1

    Input
    11
    3 2 4 2 11 2 7 5 8 10 6
    
    Expected output
    2 2 1 1 0 3 2 2 1 0 0