알고리즘/백준(BOJ)

백준 - 11286 절대값 힙

시나모온 2020. 6. 11. 02:01

문제 링크입니다 : https://www.acmicpc.net/problem/11286

 

11286번: 절댓값 힙

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

www.acmicpc.net

 

 

 

 

 

 

 

 

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int main() {
    priority_queue<pair<int, int>> pq;
    int N, input;
    scanf("%d", &N);

    for(int i = 0; i < N; i++) {
        scanf("%d", &input);
        if(input == 0) {
            if(pq.empty()) {
                printf("0\n");
            } else {
                printf("%d\n", pq.top().first * pq.top().second);
                pq.pop();
            }
        } else {
            pq.push({input < 0 ? input : -input, input < 0 ? 1 : -1});
        }
    }

    return 0;
}

 

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~