Published 2020. 12. 29. 01:48
 

1874번: 스택 수열

1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.

www.acmicpc.net

#include <iostream>
#include <string>
#include <stack>

using namespace std;
int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	stack<int> stk;
	string hist;
	int n; cin >> n;
	int m = 0;
	while (n--) {
		int input; cin >> input;
		if (m < input) {
			while (m < input) {
				stk.push(++m);
				hist += '+';
			}
			stk.pop();
			hist += '-';
		}
		else {
			if (!stk.empty()) {
				if (stk.top() == input) {
					stk.pop();
					hist += '-';
				}
				else if (stk.top() > input) {
					cout << "NO" << endl;
					return 0;
				}
			}
		}
	}

	for (char& ans : hist) { cout << ans << '\n'; }
}

'Problem set' 카테고리의 다른 글

[백준] 1934: 최소공배수  (0) 2020.12.29
[백준] 1929: 소수 구하기  (0) 2020.12.29
[백준] 1676: 팩토리얼 0의 개수  (0) 2020.12.29
[백준] 1463: 1로 만들기  (0) 2020.12.29
[백준] 1406: 에디터  (0) 2020.12.29
복사했습니다!