AcWing 830. 单调栈
原题链接
简单
作者:
鹰
,
2021-03-25 13:15:45
,
所有人可见
,
阅读 333
#include<iostream>
#include<stack>
using namespace std;
const int N = 100010;
int a[N];
int n;
stack<int> s;
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
if(s.empty())
{
cout << -1 << " ";
s.push(a[i]);
continue;
}
if(a[i] > s.top())
{
cout << s.top() << " ";
s.push(a[i]);
continue;
}
else //当前的数比栈顶小
{
while(!s.empty() && s.top() >= a[i])
{
s.pop();
}
if(!s.empty()) cout << s.top() << " ";
else cout << -1 << " ";
s.push(a[i]);
}
}
return 0;
}