Coding test/Baekjoon

[백준 1927 C++][S1] 최소 힙

HEY__ 2021. 8. 6. 11:09
728x90

⭐️난이도

Silver 1


⭐️문제

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

최소 힙을 구현하면 되는 간단한 문제이다!

 

1927번: 최소 힙

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0

www.acmicpc.net

 


⭐️ 내 풀이 &  Code

📌 C++에서는 priority_queue를 제공하므로 이를 이용하면 풀 수 있는 문제였다.

 

#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <stack>

#define INF 100000000
using namespace std;

int n; // 연산의 개수
priority_queue<int, vector<int>, greater<int>> pq; // 최소힙

int main(void) {

    scanf("%d", &n);

    for(int i = 0; i < n; i++){
        int x;
        scanf("%d", &x);
        if(x == 0){
            if(pq.empty())
                printf("0\n");
            else{
                printf("%d\n", pq.top());
                pq.pop();
            }
        }
        else{
            pq.push(x);
        }
    }

    return 0;
}
728x90