yuns

[1379] 강의실2 파이썬 python 본문

algorithms/백준

[1379] 강의실2 파이썬 python

yuuuun 2022. 5. 30. 18:56
반응형

이 문제는 예시랑 내가 짠 코드가 정확히 일치하지 않아서 로직이 잘못 된 줄 알고 계속 고민하느라 오랜 시간이 소요됐다.

문제

https://www.acmicpc.net/problem/1379

 

1379번: 강의실 2

첫째 줄에 강의의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 줄마다 세 개의 정수가 주어지는데, 순서대로 강의 번호, 강의 시작 시간, 강의 종료 시간을 의미한다. 강의

www.acmicpc.net

코드

arr에는 시작 시간 기준으로 sorting을 하고, heapq에는 끝나는 시간 기준으로 우선순위 큐 정렬하기

from heapq import heappush, heappop

pq = []
n = int(input())
room = [0] * n
arr = []
for _ in range(n):
    idx, s, e = map(int, input().split())
    arr.append([s, e, idx - 1])

arr = sorted(arr, key=lambda x:(x[0]))
ans = 0
for s, e, idx in arr:
    if pq and pq[0][0] <= s:
        room[idx] = room[pq[0][2]]
        heappop(pq)
    else:
        ans += 1
        room[idx] = ans
        
    heappush(pq, [e, s, idx])

print(ans)
for i in room[1:]:
    print(i)
반응형

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

[20419] 화살표 미로 (Easy) python 파이썬  (0) 2022.05.16
[23291] python 어항 정리  (0) 2022.03.10
[19238] python 스타트 택시  (0) 2022.03.01
[23288] python 주사위 굴리기2  (0) 2022.03.01
[23289] python 주사위 굴리기  (0) 2022.03.01
Comments