#include <iostream>
#include <stack>
using namespace std;
const int N = 1e5 + 10;
int main()
{
int n;
cin >> n;
stack<int> order;
int x;
for (int i = 0; i < n; ++i)
{
cin >> x;
while (order.size() != 0 && x <= order.top())
{
order.pop();
}
if (order.size() == 0)
{
cout << "-1" << " ";
order.push(x);
}
else
{
cout << order.top() << " ";
order.push(x);
}
}
return 0;
}
图片来自 https://www.acwing.com/user/myspace/index/55289/ 大佬的题解