题目描述
单方向单调栈
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include<stack>
using namespace std;
const int N = 100100;
int map[N];
stack<int> S;
int main()
{
int n,x;
cin >> n;
for (int i = 0; i < n; i ++ ){
cin >> x;
while(!S.empty() && S.top() >= x)S.pop();
if(!S.empty())map[i] = S.top();
else map[i] = -1;
S.push(x);
}
for (int i = 0; i < n; i ++ ){
cout << map[i] << " ";
}
return 0;
}