This page is still under construction.

Parts of this page are still being built. What you see may change.

Tower View

Interview

Time limit1.5sMemory limit1024 MB

Summary
For each building, count the buildings visible to its left and right, where a shorter or equal building is hidden behind a taller one, and report the nearest visible index.
Level

Medium7 of 10

Topics
Stack, Array, Implementation, Two pointers
Solved
No attempts yet

Problem

There are NN buildings of various heights on a straight line. From the roof of each building, you want to know how many sides of buildings are visible on either side.

For the ii-th building, buildings i−1i - 1, i−2i - 2, ..., 11 are on the left, and buildings i+1i + 1, i+2i + 2, ..., NN are on the right. The distance between adjacent buildings is the same.

Suppose the current building has height LL. Only buildings with height greater than LL are visible.

In the direction you are looking, if a building of height at most LL lies behind a building of height LL, it is hidden and not visible.

Index12345678
Height37163517
Visible buildings2x2, 4, 82, 82,4,6,82,4,82,4,6,8x

Find which buildings are visible from each building.

Input

The first line gives the number of buildings NN.

The second line gives the heights of the NN buildings, separated by spaces.

Output

For each building i(1≤i≤N)i(1 \le i \le N), output the number of visible buildings.

If the number of visible buildings is at least 1, also output the smallest index among the visible buildings closest to building ii.

Constraints

  • 1≤N≤100,0001 \le N \le 100,000
  • 1≤L≤100,0001 \le L \le 100,000

Examples1

  1. Example 1

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