Notice
Recent Posts
Recent Comments
Link
궤도
[백준] 11279번 : 최대 힙 본문
문제
풀이
힙은 보통 우선순위 큐로 구현한다.
그리고 C++ STL에 우선순위 큐가 있다.
www.cplusplus.com/reference/queue/priority_queue/?kw=priority_queue
소스코드
#include <iostream>
#include <queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
priority_queue<int> pq; //default : max heap
int N, x;
cin>>N;
for(int i=0;i<N;i++){
cin>>x;
if(x==0){
if (pq.empty())
cout<<0<<'\n';
else{
cout<<pq.top()<<'\n';
pq.pop();
}
}
else
pq.push(x);
}
}
간단하게 우선순위 큐 함수만 정리하고 끝내겠다.
#include <queue> //우선 순위 큐를 사용하기 위한 헤더
priority_queue<int> pq //int형 우선순위 큐 pq 선언. default는 최대 힙
pq.push(1) //우선순위 큐 pq에 1투입
pq.pop() //우선순위 큐 pq에서 가장 위에 있는 값을 뺌
pq.top() //우선순위 큐 pq에서 가장 위에 있는 값을 반환
pq.size() //우선순위 큐 pq의 크기 반환
pq.empty() //우선순위 큐 pq가 비어있는지 아닌지 확인
Comments