PS

BOJ 12789 : 도키도키 간식드리미

lickelon 2025. 2. 8. 22:56
  • 문제 링크 : boj.kr/12789
  • 난이도 : S3
  • 태그 : 스택

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;
    cin >> n;
    vector<int> arr(n);
    for(auto &e : arr) cin >> e;

    stack<int> _st;
    int target = 1;
    for(auto e : arr) {
        if(e == target) target++;
        else _st.push(e);
        while(!_st.empty() && _st.top() == target) {
            _st.pop();
            target++;
        }
    }
    cout << (_st.empty() ? "Nice" : "Sad");

    return 0;
}

풀이

우회로는 스택, 진입로는 큐처럼 작동한다.

진입로의 선두가 간식을 받을 수 있다면 pass, 아니라면 스택에 push한다.

이후 우회로의 선두가 간식을 받을 수 없을 때까지 우회로의 선두에게 간식을 나눠주고 pass한다.

최종적으로 우회로에 사람이 남아있다면 모두에게 간식이 주어지지 않은 것이다.

728x90

'PS' 카테고리의 다른 글

BOJ 11008 : 복붙의 달인  (0) 2025.02.10
BOJ 29719 : 브실이의 불침번 근무  (0) 2025.02.09
BOJ 13335 : 트럭  (0) 2025.02.07
BOJ 3258 : 컴포트  (0) 2025.02.06
BOJ 28447 : 마라탕 재료 고르기  (2) 2025.02.05