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

프로그래밍 공부/백준 (C++)

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

Rocketbabydolls 2024. 10. 24. 13:02

 

스택을 이용해 풀이하는 문제, 시간 제한이 1초이므로 1억번 이상의 연산은 불가능하다. 

 

 

 

#include <iostream>
#include <stack>
#include <sstream>

using namespace std;

int main() {

    int input;

    cin >> input;

    stack<int> s;
    stack<int> result;

    int arr[1000000] ;

    for(int i = 0 ; i < input ; i++)
    {
        int tmp;
        cin >> tmp;

        arr[i] = tmp;
    }

    for(int i = 0 ; i < input ; i++)
    {

        while(1)
        {
            if(s.empty())
            {
                s.push(i);
                break;
            }

            if(arr[i] > arr[s.top()])
            {
                arr[s.top()] = arr[i];
                s.pop();
            }
            else
            {
                s.push(i);
                break;
            }
        }
    }

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

    for(int i = 0 ; i < input ; i++)
    {
        cout << arr[i] <<' ';
    }



    return 0;
}