Little Jay

[C++] 백준 1874번 스택 수열 본문

알고리즘/BOJ

[C++] 백준 1874번 스택 수열

Jay, Lee 2021. 8. 2. 16:51

이 문제는 문제 자체를 이해하는데 많이 시간이 소요되었다

 

결론부터 말하자면

pop()으로 나오는 숫자들이 처음의 배열과 똑같이 되는 경우를 찾고,

아닌경우는 NO를 출력하면 되는 것이다

 

문제를 조금 친절히 설명해주면 좋겠지만 문제를 분석하는 것도 결국 능력이기에......

 

 

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

int main() {

	stack<int> st;
	int n, x;
	cin >> n;

	vector<char> v;

	int max = 0;

	while (n--) {
		cin >> x;
		if (x > max) {
			for (int i = max + 1; i <= x; i++) {
				st.push(i);
				v.push_back('+');
			}
		}
		else {
			if (st.top() != x) {
				cout << "NO" << "\n";
				return 0;
			}
		}
		st.pop();
		v.push_back('-');
		if (max < x) {
			max = x;
		}
	}

	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << "\n";
	}


	return 0;
}

 

Comments