코딩캠프/BOJ

[1927] 최소힙

코곰_ 2024. 2. 14. 15:50
// 1927 최소힙  

#include <bits/stdc++.h>

using namespace std;

int x;
int n;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n; // 100,000까지  
	
	// x는 0~2^31 
	// x가 자연수 -> 배열에 x를 추가 
	// x가 0 -> 가장 작은 값을 출력하고, 그 값을 배열에서 제거  
 
	priority_queue<int, vector<int>, greater<int>> heap; // 올림차순 - 최소힙  
	
	for(int i=0; i<n; i++){
		cin >> x;
		if(x > 0){ // 배열에 x를 추가  
			heap.push(x);
		}else{
			// 가장 작은 값 출력, 그 값을 배열에서 제거  
			if (heap.empty()) {
				cout << "0" << "\n";	
			}
			else{
				cout << heap.top() << "\n";
				heap.pop();
			}
		}	
	} 
	
	return 0;
}