Problem Solving/Baekjoon Online Judge

BOJ/백준 17952 과제는 끝나지 않아!

khj1999 2024. 8. 18. 20:54

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

 

문제 자체는 직관적이다고 생각했고 어떻게 처리를 해야할지 생각하는 것이 중요하다 생각했다.
특히, 문제를 받는 즉시 시작 이 부분이 중요했다는 생각이 들었다.

 

그 다음 문제는 예외 처리인데 스택이 비어있을때 조회를 하지 않는 코드를 처음에는 작성하지 않았었는데
시작부터 0을 넣는 테스트 케이스가 있었던거 같아서 스택이 비어있을때는 스택에서 데이터를 조회하지 않게 처리를 해줘야했었다.

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

#define SCORE first
#define TIME second

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, ans = 0;
    stack<pair<int, int>> s;
    cin >> n;

    for(int i = 0; i < n; i++){
        int order;
        cin >> order;
        if(order == 1){
            int a, t;
            cin >> a >> t;
            --t;
            if(t == 0) ans += a;
            else s.push({a, t});
        }
        else{
            if(s.empty()) continue;
            pair<int, int> tmp = s.top();
            tmp.TIME--;
            if(tmp.TIME == 0){
                s.pop();
                ans += tmp.SCORE;
            }
            else{
                s.pop();
                s.push(tmp);
            }
        }
    }
    cout << ans;
    return 0;
}