图来自:https://www.acwing.com/solution/content/27437/
参考:https://raelum.blog.csdn.net/article/details/128969669

//数组栈 
#include <iostream>
using namespace std;

const int N = 1e5+10;
int tt,stk[N];
int main(void){
	int n;
	cin >> n;
	for(int i = 0;i<n;i++){
		int x;
		cin >> x;
		while(tt && stk[tt] >= x) tt--;
		if(tt) cout << stk[tt] << ' ';
		else cout << -1 << ' ';
		stk[++tt] = x;
	}
	return 0;
}



//c++ stl栈

#include <iostream>
#include <stack>
using namespace std;
int main(void){
	int n;
	cin >> n;
	int x;
	stack<int> stk;
	while(n--){
		cin >> x;
		while(stk.size()&&stk.top()>=x) stk.pop();
		if(stk.empty()) {
			cout << "-1" << ' ';
		}else {
			cout << t.top() << ' ';
		}
		t.push(x);
	}
}```