#P2947. Next Taller Cow

    ID: 16205 Type: Default 1000ms 256MiB

Next Taller Cow

Next Taller Cow

Farmer John's N (\(1\le N\le10^5\)) cows are standing in a row, numbered from 1 to N. Cow \(i\) has height \(H_i\) (\(1\le H_i\le10^6\)).

Each cow looks to the right (toward cows with higher index numbers). We say that cow \(i\) "looks up" to cow \(j\) if \(i<j\) and \(H_i<H_j\). For each cow \(i\), the goal is to find the index of the closest cow to her right that is taller. If there is no such cow, output \(-1\).

Note: Approximately 50% of the test cases will have \(N\le 1000\).

inputFormat

The first line contains a single integer \(N\), the number of cows. The second line contains \(N\) space-separated integers, representing the heights \(H_1, H_2, \ldots, H_N\) of the cows.

outputFormat

Output a single line containing \(N\) integers. The \(i\)th integer should be the index of the first cow to the right of cow \(i\) that is taller. If no such cow exists, output \(-1\) for that position. The indices are 1-indexed.

sample

4
4 3 5 2
3 3 -1 -1