problems / 168

Alice's Melting Candle Row

medium linked-list simulation arrays 2000 ms · 256 MB

Alice's Melting Candle Row

Alice has lined up n candles on a long shelf, numbered 1 to n from left to right; candle i has height h_i. The shelf sits right next to a radiator, and the candles behave rather strangely: a candle that stands in the shadow of a taller candle on its left gets too warm and melts. Bob, who is supposed to be keeping an eye on them, wants to know how the row will look once the melting is over.

Formally, the row changes in seconds 1, 2, 3, …. At each second, look at the current row (the candles that have not melted yet, in their original order). Every candle that is strictly shorter than the candle immediately to its left in the current row melts. All these candles melt at the same moment, judged by the row as it was at the start of the second, so a candle that melts at this second can still make its right neighbour melt at this same second. Then the melted candles disappear and the remaining candles close up, keeping their order. The leftmost candle of the current row never melts.

For each candle, Bob wants the second at which it melts, or 0 if it never melts. He also wants T, the number of the last second at which at least one candle melts (T = 0 if no candle ever melts).

Input

The first line contains an integer n (1 ≤ n ≤ 10^6), the number of candles.

The second line contains n integers h_1, h_2, …, h_n (1 ≤ h_i ≤ 10^9), the heights of the candles from left to right.

Output

On the first line print T.

On the second line print n integers separated by spaces: the i-th of them is the second at which candle i melts, or 0 if it never melts.

Notes

In the first example, at second 1 the row is 10 9 7 8 6 5 3 4 2 1 and the candles of heights 9, 7, 6, 5, 3, 2, 1 melt (each is shorter than its left neighbour, even when that neighbour melts too). The row becomes 10 8 4. At second 2 both 8 and 4 melt, leaving only 10, so T = 2.

In the second example, the row goes 5 1 3 3 2 6 → 5 3 3 6 → 5 3 6 → 5 6. A candle of the same height as its left neighbour does not melt.

Example 1

Input

10
10 9 7 8 6 5 3 4 2 1

Output

2
0 1 1 2 1 1 1 2 1 1

Example 2

Input

6
5 1 3 3 2 6

Output

3
0 1 2 3 1 0

Hints · reveal one at a time if you're stuck

Hint 1

A candle's fate in a second depends only on its current left neighbour. If a candle survived second t and its left neighbour did not change during second t, can it melt at second t+1?

Hint 2

So only the candles right after a melted candle need checking in the next second. Keep the row as a linked list with a candidate list per second, and decide all melts for a second before removing anything, because melting is simultaneous.

Hint 3

Each candle melts at most once and each melt creates only a constant number of new candidates, so the whole process is linear. Be careful with equal heights, which never melt.

report an issue Prefer your own editor? sooks prep 168 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.