单调栈模板题。
单调栈与单调队列一样,都是维护了一段区间内的顺序。
然后……这个题用一个栈维护一下贪心就没了。
具体参考这一篇题解
#include <bits/stdc++.h>
#define itn int
#define gI gi
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int maxn = 3000003;
int n, a[maxn], sta[maxn], ans[maxn], topp;
int main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
n = gi();
for (int i = 1; i <= n; i+=1) a[i] = gi();
for (int i = n; i >= 1; i-=1)
{
while (topp && a[sta[topp]] <= a[i]) --topp;
ans[i] = sta[topp];
sta[++topp] = i;
}
for (int i = 1; i <= n; i+=1) printf("%d ", ans[i]);
return 0;
}