这题的难点
第一步:在每一个新的数入栈之前要思考,在栈里有没有比他大的元素,如果有,则让他退栈
这样可以确保栈永远保持单调状态
第二步:此时栈顶元素就是符合题意的元素,将其输出。
第三步:将最初输入的元素送入栈,起到一种更新栈的作用
#include <iostream>
using namespace std;
const int N=1e5+10;
int s[N],tt;
int main()
{
int n;
cin>>n;
while(n--)
{
int x;
cin>>x;
while(tt&&s[tt]>=x) tt--;
if(tt==0) cout<<-1<<' ';
else cout<<s[tt]<<' ';
s[++tt]=x;
}
return 0;
}