https://www.acmicpc.net/problem/1874
간단한 자료구조인 stack을 이용해서 푸는 문제이다.
#include <iostream>
#include <stack>
#include <vector>
#include <stdlib.h>
using namespace std;
int main() {
int N;
cin >> N;
stack<int> st;
vector<bool> arr;
int count = 1;
for (int i = 0; i < N; ++i) {
int tmp;
cin >> tmp;
while (true) {
if (st.empty() && count <= tmp) {
arr.emplace_back(true);
st.push(count++);
}
if (st.empty()) {
cout << "NO";
exit(0);
}
if (st.top() == tmp) {
arr.emplace_back(false);
st.pop();
break;
}
else if (st.top() > tmp) {
st.pop();
}
else {
if (count <= tmp) {
arr.emplace_back(true);
st.push(count++);
}
else {
cout << "NO";
exit(0);
}
}
}
}
for (vector<bool>::iterator iter = arr.begin(); iter != arr.end(); ++iter) {
if (*iter == true)
cout << "+" << "\n";
else
cout << "-" << "\n";
}
return 0;
}
stack의 기본구조를 이해시켜주는 문제였다.
'백준' 카테고리의 다른 글
2164번 (silver4) (0) | 2025.01.14 |
---|---|
17298번 (gold 4) (0) | 2025.01.14 |
11003번 (platinum 5) (0) | 2025.01.13 |
1253번 (gold 4) (0) | 2025.01.12 |
1940번 (silver 4) (0) | 2025.01.11 |