코딩테스트

[백준] 온라인 저지 1874번 : 스택수열(C++), 스택

yeonii_ 2025. 5. 29. 00:02

https://www.acmicpc.net/problem/1874

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<int> V(N);
    for (int i = 0; i < N; ++i)
    {
        cin >> V[i];
    }

    stack<int> s;
    vector<string> answers;
    
    int top = 1;
    int idx = 0;

    while(idx < N)
    {
        if (s.empty() || V[idx] > s.top())
        {
            if (top <= N)
            {
                s.push(top++);
                answers.push_back("+");
            }
            else
            {
                cout << "NO";
                return 0;
            }
        }
        else if (V[idx] == s.top())
        {
            s.pop();
            answers.push_back("-");
            ++idx;
        }
        else
        {
            cout << "NO";
            return 0;
        }
    }

    for (string answer : answers)
    {
        cout << answer << "\n";
    }
}