-
Notifications
You must be signed in to change notification settings - Fork 5
Minjeong / 6월 1주차 / 3문제 #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
21 changes: 21 additions & 0 deletions
21
minjeong/Heap/2025-06-02-[백준]-#14698-전생했더니슬라임연구자였던건에대하여(Hard).py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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제한이라 메모리가 넉넉해서 문제가 되지는 않았는데 더 적은 메모리에서는 이런 식으로 정답 저장에 쓰이는 메모리를 줄여줄 필요가 있을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋은 정보 알려주셔서 감사해요!! 앞으로 이 부분 참고해서 작업해보도록 하겠습니다!