- //单调栈 数组实现 栈存的是下标 推荐这种方法
- #include
- using namespace std;
- using ll = long long;
- const int N = 2e5 + 9;
- ll a[N], l[N], st[N], top;
-
- void solve()
- {
- int n; cin >> n;
- for (int i = 1; i <= n; ++i) cin >> a[i];
-
- for (int i = 1; i <= n; ++i)
- {
- while (top && a[st[top]] >= a[i]) top--;
- if (top) l[i] = a[st[top]];
- else l[i] = -1;
- st[++top] = i;
- }
- for (int i = 1; i <= n; ++i) cout << l[i] << " ";
- }
-
- int main()
- {
- ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- solve();
- return 0;
- }