diff --git a/janghw0126/README.md b/janghw0126/README.md index 5cecd87..d9622ad 100644 --- a/janghw0126/README.md +++ b/janghw0126/README.md @@ -12,4 +12,5 @@ | 8차시 | 2024.8.19 | 최소 힙 | N번째 큰 수 |[#145](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/147) | | 9차시 | 2024.8.25 | BFS | 숨바꼭질 |[#149](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/149) | | 10차시 | 2024.9.9 | 위상정렬 | 줄 세우기 |[#158](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/158) | +| 11차시 | 2024.9.17 | 우선순위 큐 | 이중 우선순위 큐 |[#160](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/160) | --- diff --git "a/janghw0126/\355\236\231/\354\235\264\354\244\221 \354\232\260\354\204\240\354\210\234\354\234\204 \355\201\220.py" "b/janghw0126/\355\236\231/\354\235\264\354\244\221 \354\232\260\354\204\240\354\210\234\354\234\204 \355\201\220.py" new file mode 100644 index 0000000..5d7a7df --- /dev/null +++ "b/janghw0126/\355\236\231/\354\235\264\354\244\221 \354\232\260\354\204\240\354\210\234\354\234\204 \355\201\220.py" @@ -0,0 +1,42 @@ +import sys +import heapq + +def solve(): + input = sys.stdin.readline + T = int(input()) + + for _ in range(T): + min_heap = [] # 최소 힙 + max_heap = [] # 최대 힙 (음수로 변환하여 저장) + count = int(input()) # 명령어 수 + status = [1] * count # 작업 상태 추적 (1: 유효, 0: 삭제됨) + + for i in range(count): + command, value = input().split() + value = int(value) + + if command == "I": # 삽입 연산 + heapq.heappush(min_heap, (value, i)) + heapq.heappush(max_heap, (-value, i)) + elif command == "D": # 삭제 연산 + if value == -1: # 최소값 삭제 + if min_heap: + status[heapq.heappop(min_heap)[1]] = 0 + elif value == 1: # 최대값 삭제 + if max_heap: + status[heapq.heappop(max_heap)[1]] = 0 + + # 유효하지 않은 값 제거 + while min_heap and status[min_heap[0][1]] == 0: + heapq.heappop(min_heap) + while max_heap and status[max_heap[0][1]] == 0: + heapq.heappop(max_heap) + + # 결과 출력 + if not min_heap or not max_heap: + print("EMPTY") + else: + print(-max_heap[0][0], min_heap[0][0]) + +if __name__ == "__main__": + solve() \ No newline at end of file