백준 11286 절댓값 힙 / C++

이유참치·2025년 12월 15일

백준

목록 보기
139/249

문제 : 11286

풀이 point

문제에 맞는 비교 구조체를 만들어 우선순위 큐 생성에 넣는다.

코드

//백준 11286, 절댓값 힙

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

struct cmp{ // 정렬 기준 바꾸는 함수
    bool operator()(int a, int b){
        if(std::abs(a) == std::abs(b))
            return a > b; // 절대값이 같은 경우 가장 작은 원소로
        return std::abs(a) > std::abs(b); // 절대값이 작은 원소로
    }
};

int main (){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    int N, x;
    std::cin >> N;
    std::priority_queue<int, std::vector<int>, cmp> pq;

    while(N--){
        std::cin >> x;
        if(x != 0) pq.push(x);
        else{
            if(pq.empty()) std::cout << 0 << '\n';
            else{
                std::cout << pq.top() << '\n';
                pq.pop();
            }
        }
    }

    return 0;
}
profile
임아리 - 대학생

0개의 댓글