Look Up

Interview

Time limit1sMemory limit128 MB

Summary
For each cow, find the smallest index j > i with H_j > H_i, or print 0 if none exists.
Level

Medium4 of 10

Topics
Stack, Array, Implementation
Solved
No attempts yet

Problem

Farmer John's NN cows (1≤N≤1000001 \le N \le 100000), conveniently numbered 1…N1 \ldots N, are standing in a row. Cow ii has height HiH_i (1≤Hi≤10000001 \le H_i \le 1000000).

Each cow looks toward the cows with higher index numbers. We say cow ii "looks up" to cow jj if i<ji < j and Hi<HjH_i < H_j. For each cow ii, determine the index of the first cow in the row (the one with the smallest index) that cow ii looks up to.

Input

  • Line 1: a single integer NN.
  • Lines 2 to N+1N+1: line i+1i+1 contains the single integer HiH_i.

Output

  • Lines 1 to NN: line ii contains the smallest index of a cow that cow ii looks up to. If no such cow exists, print 00.

Hint

Cows 11 and 22 both look up to cow 33; cows 44 and 55 both look up to cow 66; and cows 33 and 66 do not look up to any cow.

Examples1

  1. Example 1

    Input
    6
    3
    2
    6
    1
    1
    2
    
    Expected output
    3
    3
    0
    6
    6
    0