单调栈

· · 个人记录

#include <bits/stdc++.h>
using namespace std;
const int N = 3000005;
int n, a[N], stk[N], ans[N], top;
signed main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    for (int i = n; i; --i) {
        while (a[stk[top]] <= a[i] && top) -- top;
        ans[i] = stk[top];
        stk[++ top] = i;
    }
    for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
    return 0;
}