Skip to content

Commit 58da966

Browse files
authored
Merge pull request #219 from AlgorithmStudy-Allumbus/minjeong3
Minjeong / 6월 1주차 / 3문제
2 parents 57b6bec + 8c38a8c commit 58da966

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import heapq
2+
3+
def solution(jobs):
4+
jobs.sort() # 요청시간 기준 정렬
5+
job_len = len(jobs)
6+
i = 0 # jobs 인덱스
7+
end_time = 0 # 현재 시간
8+
return_time = 0 # 작업 반환 시간
9+
count = 0 # 작업 처리한 개수
10+
11+
heap = []
12+
13+
while count < job_len:
14+
# 현재 시간에 요청된 작업 처리
15+
while i < job_len and jobs[i][0] <= end_time:
16+
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
17+
i += 1
18+
19+
# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
20+
if len(heap) > 0:
21+
work_time, start_time, num = heapq.heappop(heap)
22+
end_time += work_time
23+
return_time += end_time - start_time
24+
count += 1
25+
else:
26+
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
27+
end_time = jobs[i][0]
28+
29+
return return_time // job_len
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
import heapq
3+
input = sys.stdin.readline
4+
5+
T = int(input())
6+
for _ in range(T):
7+
total = 1
8+
N = int(input())
9+
heap = list(map(int, input().split()))
10+
if N == 1:
11+
print(1)
12+
continue
13+
14+
heapq.heapify(heap)
15+
16+
while len(heap) > 1:
17+
energy = heapq.heappop(heap) * heapq.heappop(heap)
18+
total *= energy
19+
heapq.heappush(heap, energy)
20+
21+
print(total % 1000000007)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import sys
2+
import heapq
3+
input = sys.stdin.readline
4+
5+
N = int(input())
6+
cards = [int(input()) for _ in range(N)]
7+
answer = 0
8+
heapq.heapify(cards)
9+
10+
while len(cards) > 1:
11+
a = heapq.heappop(cards)
12+
b = heapq.heappop(cards)
13+
answer += a + b
14+
heapq.heappush(cards, a+b)
15+
16+
print(answer)

0 commit comments

Comments
 (0)