본문 바로가기

PS

boj 17298 c++ [백준, 오큰수]

#include <bits/stdc++.h>
using namespace std;

int n, a[1000002];

void solve() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    stack<int> st, ans;
    for (int i = n - 1; i >= 0; --i) {
        while (!st.empty() && st.top() <= a[i]) {
            st.pop();
        }
        if (st.empty()) {
            ans.push(-1);
        } else {
            ans.push(st.top());
        }
        st.push(a[i]);
    }
    while (!ans.empty()) {
        cout << ans.top() << ' ';
        ans.pop();
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}