1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <queue>
 
using namespace std;
 
int main() {
    int n;
    scanf("%d"&n);
 
    priority_queue<intvector<int>, less<int>> na;  //음수. 최대힙
    priority_queue<intvector<int>, greater<int>> po;  //양수. 최소힙
 
    for (int i = 0; i < n; i++) {
        int temp;
        scanf("%d"&temp);
 
        if (temp == 0) {
            //pop
            if (na.empty() && po.empty())
                printf("0\n");
            else if (na.empty() && !po.empty()) {
                printf("%d\n"po.top());
                po.pop();
            }
            else if (!na.empty() && po.empty()) {
                printf("%d\n"na.top());
                na.pop();
            }
            else {
                //둘 다 데이터 있음
                if (na.top() * (-1< po.top()) {
                    printf("%d\n"na.top());
                    na.pop();
                }
                else if (na.top() * (-1> po.top()) {
                    printf("%d\n"po.top());
                    po.pop();
                }
                else {
                    //절대값이 같으면
                    printf("%d\n"na.top());
                    na.pop();
                }
 
            }
        }
        else {
            //push
            if (temp < 0)  //음수면
                na.push(temp);
            else  //양수면
                po.push(temp);
        }
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

음수는 최대힙에, 양수는 최소힙에 따로 저장

 

 

'Baekjoon' 카테고리의 다른 글

[#2667] 단지번호붙이기  (0) 2020.03.31
[#1260] DFS와 BFS  (0) 2020.03.31
[#1927] 최소 힙  (0) 2020.03.17
[#11279] 최대 힙  (0) 2020.03.17
[#2805] 나무 자르기  (0) 2020.03.17

+ Recent posts