自己手写
#include<iostream>
#include<stack>
using namespace std;
const int N = 100010;
int n;
int a[N], b[N];
stack<int> s;
int main()
{
cin >> n;
for(int i = 1 ; i <= n ; i ++ )
scanf("%d",&a[i]);
s.push(a[1]);
b[1] = -1;
for (int i = 2 ;i <= n ; i ++ )
{
if (a[i] > s.top())
{
b[i] = s.top();
s.push(a[i]);
}
else
{
while ( !s.empty() && s.top() > a[i] )
{
s.pop();
}
b[i] = s.empty() ? -1 : s.top();
s.push(a[i]);
}
}
for ( int i = 1 ; i <= n ; i ++)
printf("%d ",b[i]);
return 0;
}