알고리즘

백준[17298번] 오큰수 C++

moongi 2023. 11. 7. 11:39
반응형

난이도: G4

문제풀이 : stack 이용

 

#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;

    stack<pair<int, int>> stk;
    vector<int> res(n, -1);

    int x;
    for (int i = 0; i < n; i++)
    {
        cin >> x;

        while (!stk.empty() && stk.top().second < x)
        {
            res[stk.top().first] = x;
            stk.pop();
        }

        stk.push({i, x});
    }

    for (auto v : res)
        cout << v << " ";

    return 0;
}
반응형

'알고리즘' 카테고리의 다른 글

[백준 15683번] 감시 C++  (0) 2023.12.18
[백준 12100번] 2048 C++  (2) 2023.11.18
백준[2230번] 수 고르기 C++  (0) 2023.11.06
백준[1351번] 무한수열 C++  (0) 2023.11.06
백준[15663번] N과 M(9)  (2) 2023.10.31