#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <cstdlib>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 2000010;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
//维护第i个数前/后第一个比它大或小的数
//前后往前扫或从前往后扫(根据题意来决定);
int n;
const int N = 3e6 + 10;
int a[N];
int f[N];
stack<int> st;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) {
st.pop();
}
f[i] = st.empty() ? 0 : st.top();
st.push(i);
}
for (int i = 1; i <= n; i++) {
printf("%d ", f[i]);
}
return 0;
}
P5788 【模板】单调栈
最新推荐文章于 2023-02-07 13:09:14 发布