Chain
InterviewTime limit2sMemory limit512 MB
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 integers . For any element () we find the first element larger than that is placed to the right of (if such exists). Denote it by . Then do the same with and denote the found element by , and so on until we run out of the given sequence. Thus formed subsequence we call the chain beginning at index .
Write program chain that outputs for any index the length of the corresponding chain that begins at index .
Input
On the first line of the standard input, the value of 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
- , for each .