Problem6019--单调栈 I

6019: 单调栈 I

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 256 MiB

Description

给出项数为 $n$ 的整数数列 $a_1,\ \dots,\ a_n$。
定义函数 $f(i)$ 代表数列中第 $i$ 个元素之后第一个大于 $a_i$ 的元素的下标,即 $f(i)=\min_{i<j \leq n,\ a_j > a_i} \{j\}$。若不存在,则 $f(i)=0$。
试求出 $f(1 \dots n)$。

Input

第一行一个正整数 $n$。
第二行 $n$ 个正整数 $a_1,\ \dots,\ a_n$

Output

一行 $n$ 个整数 $f(1 \dots n)$ 的值。

Constraints

对于 $30\%$ 的数据,$n≤100$;
对于 $60\%$ 的数据,$n\leq 5 \times 10^3$;
对于 $100\%$ 的数据,$1 \le n\leq 3\times 10^6,\ 1\leq a_i\leq 10^9$。

Sample 1 Input

5
1 4 2 3 5

Sample 1 Output

2 5 4 5 0

HINT

题目来源:洛谷 P5788

Source/Category

数据结构 2.3.单调栈