본문 바로가기
알고리즘/백준

[백준]230207 문제풀이

by stubborngastropod 2023. 2. 7.
728x90

1374. 강의실

import sys
import heapq

classlist = []
heap = []
ans_list = []
N = int(sys.stdin.readline())
for i in range(N):
    classnum, start, end = map(int, sys.stdin.readline().split())
    heapq.heappush(classlist, [start, end])

while classlist:
    a = heapq.heappop(classlist)
    a[0], a[1] = a[1], a[0]
    while heap and heap[0][0] <= a[1]:
        heapq.heappop(heap)
    heapq.heappush(heap, a)
    ans_list.append(len(heap))

print(max(ans_list))

1927. 최소 힙

import heapq
import sys

N = int(sys.stdin.readline())
heap = []
for i in range(N):
    a = int(sys.stdin.readline())
    if a == 0:
        if heap:
            print(heapq.heappop(heap))
        else:
            print(0)
    else:
        heapq.heappush(heap, a)

11279. 최대 힙

import heapq
import sys

N = int(sys.stdin.readline())
heap = []
for i in range(N):
    a = int(sys.stdin.readline())
    if a == 0:
        if heap:
            print(heapq.heappop(heap)[1])
        else:
            print(0)
    else:
        heapq.heappush(heap, (-a, a))

heapq 라이브러리 배우고 힙 연습하려고 푼 문제들. 우선순위 큐 개념만 이해하고 라이브러리 쓸 줄 아니 골드 문제도 쉽게 쉽게 넘어간다. 기본적으로 값이 낮은 것들에 우선순위를 두므로 최대 힙 등 예외의 경우에는 heappush 과정에서 push되는 값을 조금만 조정해주면 된다.

728x90

'알고리즘 > 백준' 카테고리의 다른 글

[백준]230209 문제풀이  (0) 2023.02.09
[백준]230208 문제풀이  (0) 2023.02.08
[백준]230206 문제풀이  (0) 2023.02.06
[백준]230204-05 문제풀이  (0) 2023.02.05
[백준]230203 문제풀이  (0) 2023.02.04

댓글