백준 17298 오큰수 / C++

이유참치·2025년 12월 15일

백준

목록 보기
175/249

문제 : 17298

풀이 point

스택의 top과 들어오는 arr에 들어오는 값을 비교하면서 arr값이 크다면 오큰수를 설정해주고 pop해준다.

코드

//백준 17298, 오큰수

#include <iostream>
#include <stack>

std::stack<int> s;
int arr[1'000'000];
int print[1'000'000];

int main (){

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
    
    int N;
    std::cin >> N;

    for(int i{0}; i<N; ++i) std::cin >> arr[i];

    for(int i{0}; i<N; ++i){
        while(!s.empty() && arr[s.top()] < arr[i]){
            print[s.top()] = arr[i];
            s.pop();
        }
        s.push(i);
    }

    while(!s.empty()){
        print[s.top()] = -1;
        s.pop();
    }

    for(int i{0}; i<N; ++i) std::cout << print[i] << ' ';

    return 0;
}
profile
임아리 - 대학생

0개의 댓글