PS/BOJ
[1874] 스택 수열 (C++)
히명
2024. 4. 22. 12:29
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
stack<int> st;
vector<char> answer;
int num = 1;
int N; cin >> N;
for (int i = 0; i < N; i++) {
int a; cin >> a;
if (!st.empty() && st.top() == a) {
st.pop();
answer.push_back('-');
}
else if (a >= num) {
while (a >= num) {
st.push(num++);
answer.push_back('+');
}
st.pop();
answer.push_back('-');
}
else if (!st.empty() && st.top() > a) {
cout << "NO" << endl;
return 0;
}
}
for (auto x : answer)
cout << x << "\n";
return 0;
}
1. 스택이 비어있지 않고 top이 입력받은 값과 같으면 pop, ans에 '-' 추가
2. num의 값이 입력받은 값보다 작으면 같아질때까지 num++을 push, ans에 '+' 추가
3. 스택이 비어있지 않고 top이 입력받은 값보다 크면 "NO" 출력
