Skip to content

Commit 7bdd446

Browse files
committed
feat: 2월 1주차 과제 문제 답변 업로드 (BOJ #19638. 센티와 마법의 뿅망치 (실버1))
1 parent 8ca98f6 commit 7bdd446

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

_WeeklyChallenges/W10-[Priority-Queue]/Assignment_BOJ_19638_센티와마법의뿅망치.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,66 @@
44
유형: Priority Queue, Data Structure
55
'''
66

7-
# 과제 문제의 답은 PR 올리는 날에 함께 올라갈 예정입니다.
7+
'''
8+
풀이1
9+
'''
10+
import sys
11+
import heapq
12+
13+
input = sys.stdin.readline
14+
15+
# 입력
16+
N, H, T = map(int, input().split()) # N: 거인의 나라 인구 수, H: 센티의 키, T: 뿅망치 횟수 제한
17+
heights = []
18+
19+
for _ in range(N):
20+
heapq.heappush(heights, -int(input())) # 최대힙을 위해 음수로 저장
21+
22+
cnt = 0 # 사용한 뿅망치 개수
23+
24+
# 뿅망치 사용
25+
while cnt < T:
26+
tallest = -heapq.heappop(heights) # 가장 큰 키를 가져옴
27+
28+
if tallest < H: # 센티보다 작은 거인이 나오면 즉시 종료
29+
print(f"YES\n{cnt}")
30+
break
31+
32+
# 키가 1이면 더 줄일 수 없음
33+
if tallest == 1:
34+
heapq.heappush(heights, -1)
35+
else:
36+
heapq.heappush(heights, -(tallest // 2))
37+
38+
cnt += 1
39+
else:
40+
# 뿅망치 사용을 모두 소진한 경우
41+
tallest_after = -heapq.heappop(heights) # 남아 있는 가장 큰 키
42+
if tallest_after < H:
43+
print(f"YES\n{cnt}")
44+
else:
45+
print(f"NO\n{tallest_after}")
46+
47+
'''
48+
풀이2 (최적화된 풀이로, heapreplace()를 사용하여 heappop()과 heappush()를 한 번의 연산으로 처리함)
49+
출처: https://velog.io/@hygge/Python-%EB%B0%B1%EC%A4%80-19638-%EC%84%BC%ED%8B%B0%EC%99%80-%EB%A7%88%EB%B2%95%EC%9D%98-%EB%BF%85%EB%A7%9D%EC%B9%98-Heapq
50+
'''
51+
import sys, heapq
52+
input = sys.stdin.readline
53+
54+
n, h, t = map(int, input().split())
55+
giants = [-int(input()) for _ in range(n)]
56+
heapq.heapify(giants)
57+
cnt = 0
58+
59+
for i in range(t):
60+
if -giants[0] == 1 or -giants[0] < h:
61+
break
62+
else:
63+
heapq.heapreplace(giants, -(-giants[0] // 2))
64+
cnt += 1
65+
66+
if -giants[0] >= h:
67+
print('NO', -giants[0], sep='\n')
68+
else:
69+
print('YES', cnt, sep='\n')

0 commit comments

Comments
 (0)