알고리즘

백준 11279: 최대 힙 [C++]

avocado8 2024. 7. 15. 22:40

 

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

 

최소힙 문제에서 우선순위 조건만 바꾸면 됨..

날먹

솔브드 클래스 필수문제에 둘다 있는걸 어케~

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

priority_queue<int, vector<int>, less<>> pq;

int solution(int x){
    int answer;
    if(x==0){
        if(pq.empty()) answer = 0;
        else {
            answer = pq.top();
            pq.pop();
        }
    }
    else {
        pq.push(x);
    }
    return answer;
}

int main()
{
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, x;
    
    cin >> n;
    while(n--){
        cin >> x;
        if(x==0){
            if(pq.empty()) cout << 0 << '\n';
            else {
                cout << pq.top() << '\n';
                pq.pop();
            }
        }
        else {
            pq.push(x);
        }
    }
    
    return 0;
}