알고리즘

C++ 알고리즘 - 백준 12789 도키도키 간식드리미

마루설아 2025. 1. 12. 20:47

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

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

void CPP_INIT() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

int main(void) {
	CPP_INIT();

	int input;
	int num;
	int s = 1;
	stack<int> st;

	cin >> input;

	for (int i = 0; i < input; i++) {
		while (!st.empty() && s == st.top()) {
			s++;
			st.pop();
		}
		
		cin >> num;

		if (s != num) {
			if (!st.empty() && st.top() == num) {
				st.pop();
				s++;
			}

			else	st.push(num);
		}

		else {
			s++;
		}
	}

	while (!st.empty()) {
		if (s == st.top()) {
			st.pop();
			s++;
		}

		else break;
	}

	if (st.empty()) cout << "Nice";
	else cout << "Sad";
}