[백준] 1927 최소 힙

Peace·2021년 4월 27일
0

[백준] 1927 최소 힙

문제 링크: https://www.acmicpc.net/problem/1927

문제

입출력

문제 접근

단순히 heap이 어떻게 c++에서 쓰여지는 지 볼 수 있는 문제였다.

코드 구현(c++)

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

using namespace std;

priority_queue<int, vector<int>, greater<int> > heap;

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

    int N, temp;
    cin >> N;
    for(int i = 0 ; i < N ; i++){
        cin >> temp;
        if(temp != 0) heap.push(temp);
        if(temp == 0){
            if(!heap.empty()){
                cout << heap.top() << "\n";
                heap.pop();
            }
            else{
                cout << "0\n" ;
            }
        }
    }
}
profile
https://peace-log.tistory.com 로 이사 중

0개의 댓글

관련 채용 정보