#P5788. Next Greater Element Index

    ID: 19016 Type: Default 1000ms 256MiB

Next Greater Element Index

Next Greater Element Index

Given an integer sequence of length \( n \) with elements \( a_1, a_2, \dots, a_n \), define the function \( f(i) \) as the index of the first element after \( a_i \) that is greater than \( a_i \), i.e.,

\[ f(i) = \min_{i a_i} \{ j \} \]

and if no such element exists then \( f(i) = 0 \).

Compute \( f(1), f(2), \dots, f(n) \).

inputFormat

The first line contains an integer \( n \) representing the number of elements in the sequence. The second line contains \( n \) integers \( a_1, a_2, \dots, a_n \) separated by spaces.

outputFormat

Output \( n \) numbers where the \( i \)th number is \( f(i) \). The numbers should be separated by spaces.

sample

5
1 3 2 4 5
2 4 4 5 0