1927번: 최소 힙
첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이�
www.acmicpc.net
greater <int> 이용
#include <cstdio>
#include <queue>
using namespace std;
int main(void){
priority_queue <int, vector<int>, greater<int>> q;
int N, x;
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf("%d", &x);
if(x==0){
if(q.empty())
printf("0\n");
else{
printf("%d\n", q.top());
q.pop();
}
}
else
q.push(x);
}
}
사용자 지정 함수 이용
#include <cstdio>
#include <queue>
using namespace std;
class Comp{
public:
bool operator()(const int &a, const int &b){
return a>b;
}
};
int main(void){
priority_queue <int, vector<int>, Comp> q;
int N, x;
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf("%d", &x);
if(x==0){
if(q.empty())
printf("0\n");
else{
printf("%d\n", q.top());
q.pop();
}
}
else
q.push(x);
}
}
'BOJ 길라잡이' 카테고리의 다른 글
[C/C++ 백준 11286번] 절댓값 힙 (Silver 1) (0) | 2020.10.14 |
---|---|
[C/C++ 백준 11279번] 최대 힙 (Silver 2) (0) | 2020.10.14 |
[C/C++ 백준 11047번] 동전 0 (Silver 1) (0) | 2020.10.14 |
[C/C++ 백준 2293번] 동전 1 (Silver 1) (0) | 2020.10.13 |
[C/C++ 백준 15711번] 환상의 짝꿍 (Gold 4) (0) | 2020.10.13 |