백준 문풀

백준 11279

조강학 2024. 5. 6. 23:36

최대 힙

최대 힙을 사용하기 위해 queue 자료구조를 사용하였다.

pop()은 최대값을 삭제할 뿐 최대값을 반환해주지는 않으므로 top()을 통해 최대값을 반환받은 후 pop()으로 제거해주었다.

#include<iostream>
#include<queue>
#include<string>

using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    priority_queue<int>max_heap;
    long long num;
    int x;
    cin >> num;
    for (int i = 0;i < num;i++) {
        cin >> x;
        if (x == 0) {
            if (max_heap.empty()) {
                cout << 0<<"\n";
            }
            else {
                cout << max_heap.top()<<"\n";
                max_heap.pop();
            }
        }
        else {
            max_heap.push(x);
        }
    }
    return 0;
}

 

'백준 문풀' 카테고리의 다른 글

백준 1764  (0) 2024.05.06
백준 11478  (0) 2024.05.06
백준 7785  (0) 2024.05.06
백준 5555  (1) 2024.03.15
백준 2908  (3) 2024.03.15