AcWing 830. 单调栈
原题链接
简单
作者:
自律
,
2021-07-17 20:03:23
,
所有人可见
,
阅读 255
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int q[N],s[N];
int top = -1;
void push(int x)
{
s[++top] = x;
}
void pop()
{
top--;
}
int main()
{
int n;
cin>>n;
for(int i = 0;i < n;i ++) cin>>q[i];
for(int i = 0;i < n;i ++)
{
if(top == -1)
{
cout<<-1<<" ";
push(q[i]);
}
else if(q[i] > s[top])
{
cout<<s[top]<<" ";
push(q[i]);
}
else
{
while(q[i] <= s[top]&&top > -1) pop();
if(top == -1) cout<<-1<<" ";
else cout<<s[top]<<" ";
push(q[i]);
}
}
return 0;
}