큐(Queue)

백준 1927번: 최소힙

cepiloth 2018. 6. 17. 13:54
반응형

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


1. 문제 요약

최소힙 구현 문제


2. 알고리즘

priority_queue 사용 greater<int> 사용하여 최소힙으로 사용


3. 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
using namespace std;
 
int main() {
    
    std::ios::sync_with_stdio(false); cin.tie(0);
 
    priority_queue<intvector<int>, greater<int>> q;
 
    int n; cin >> n;
 
    while(n--) {
        int cand; cin >> cand;
        
        if (cand == 0) {
            if (q.empty()) {
                cout << 0 << "\n";
            } else {
                cout << q.top() << "\n";
                q.pop();
            }
        } else {
            q.push(cand);
        }        
    }
    return 0;
}
cs

반응형