https://www.acwing.com/problem/content/832/
    image.png
    image.png
    image.png

    1. #include <iostream>
    2. using namespace std;
    3. const int N = 1e5 + 10;
    4. int n;
    5. int stk[N], tt;
    6. int main()
    7. {
    8. cin >> n;
    9. while (n--)
    10. {
    11. int x;
    12. cin >> x;
    13. while (tt && stk[tt] >= x) tt--;
    14. if (tt) cout << stk[tt] << ' ';
    15. else cout << "-1" << ' ';
    16. stk[++tt] = x;
    17. }
    18. puts("");
    19. return 0;
    20. }