AcWing 830. 单调栈
原题链接
简单
作者:
piaofan
,
2021-01-25 10:02:34
,
所有人可见
,
阅读 226
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, x, ans[maxn], cnt;
int main (void) {
stack<int> st;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
if (st.empty()) { st.push(x); ans[cnt++] = -1;}
else {
while (st.top() >= x) {st.pop(); if (st.empty()) break;}
if (st.empty()) { st.push(x); ans[cnt++] = -1;}
else { ans[cnt++] = st.top(); st.push(x); }
}
}
for (int i = 0; i < n; i++) cout << ans[i] << " ";
return 0;
}