Files
python/TangDou/AcWing/Stack/830.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

28 lines
483 B
C++

#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 1e5 + 10;
int a[N];
int stk[N], tt;
int res[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = n; i >= 1; i--) {
while (tt && a[stk[tt]] > a[i]) {
res[stk[tt]]=i;
tt--;
}
stk[++tt]=i;
}
for (int i = 1; i <= n; i++)
res[i]?printf("%d ",a[res[i]]):printf("-1 ");
return 0;
}