AcWing 830. 可读性更高的版本,速度一样
原题链接
简单
C++ 代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int stack[N], tt;
void push(int x)
{
stack[tt++] = x;
}
void pop()
{
tt--;
}
int top()
{
return stack[tt - 1];
}
bool empty()
{
return tt == 0;
}
int main()
{
int n;
cin >> n;
while (n--)
{
int x;
cin >> x;
if (empty())
{
push(x);
cout << -1 << " ";
continue;
}
else
{
if (x <= top())
{
while (x <= top() && !empty())pop();
empty() ? cout << -1 << " " : cout << top() << " ";
push(x);
}
else cout << top() << " " , push(x);
}
}
return 0;
}