Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions minjeong/Heap/2025-06-01-[PGS]-디스크컨트롤러.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import heapq

def solution(jobs):
jobs.sort() # 요청시간 기준 정렬
job_len = len(jobs)
i = 0 # jobs 인덱스
end_time = 0 # 현재 시간
return_time = 0 # 작업 반환 시간
count = 0 # 작업 처리한 개수

heap = []

while count < job_len:
# 현재 시간에 요청된 작업 처리
while i < job_len and jobs[i][0] <= end_time:
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
i += 1

# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
if len(heap) > 0:
work_time, start_time, num = heapq.heappop(heap)
end_time += work_time
return_time += end_time - start_time
count += 1
else:
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
end_time = jobs[i][0]

return return_time // job_len
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
import heapq
input = sys.stdin.readline

T = int(input())
for _ in range(T):
total = 1
N = int(input())
heap = list(map(int, input().split()))
if N == 1:
print(1)
continue

heapq.heapify(heap)

while len(heap) > 1:
energy = heapq.heappop(heap) * heapq.heappop(heap)
total *= energy
heapq.heappush(heap, energy)

print(total % 1000000007)
Comment on lines +16 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제 PR에 작성해주신것 처럼 저는 while문 안에서 계속 1,000,000,007로 나머지 연산해주었어요. 파이썬은 2^0 부터 2^30 까지는 28바이트를 쓰고 그 이후부터는 2^30마다 4바이트씩 커지는데, 이 문제는 10억 이상 넘어가는 경우가 많이 나올수 있다고 생각해서 메모리를 아끼기 위해서 중간중간 계속 1,000,000,007로 나머지 연산 해주었습니다. 이 문제는 512MB제한이라 메모리가 넉넉해서 문제가 되지는 않았는데 더 적은 메모리에서는 이런 식으로 정답 저장에 쓰이는 메모리를 줄여줄 필요가 있을 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 정보 알려주셔서 감사해요!! 앞으로 이 부분 참고해서 작업해보도록 하겠습니다!

16 changes: 16 additions & 0 deletions minjeong/Heap/2025-06-06-[백준]-#1715-카드정렬하기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys
import heapq
input = sys.stdin.readline

N = int(input())
cards = [int(input()) for _ in range(N)]
answer = 0
heapq.heapify(cards)

while len(cards) > 1:
a = heapq.heappop(cards)
b = heapq.heappop(cards)
answer += a + b
heapq.heappush(cards, a+b)

print(answer)