Algorithm/SW Expert Academy

[Python] 5177. 이진힙

느낌표 공장장 2021. 9. 24. 17:35
tc = int(input())
for idx in range(1, tc+1):
n = int(input()) # 목표 노드
node = [0] + list(map(int, input().split())) # 노드들 받아오기
answer = 0
# 위치 바꾸기
for i in range(1, n+1):
while node[i//2] > node[i]: # 부모가 나보다 클때 바꿔주기
node[i//2], node[i] = node[i], node[i//2]
i //= 2 # 다음 조상도 검토
# 조상 노드 다 더하기
p = n//2 # n의 부모부터 시작이니까
while p > 0:
answer += node[p]
p //= 2
print(node)
print('#{} {}'.format(idx, answer))

'Algorithm > SW Expert Academy' 카테고리의 다른 글

[Python] 1240. 단순 2진 암호코드  (0) 2021.09.29
[Python] 5178. 노드의 합  (0) 2021.09.24
[Python] 5176. 이진탐색  (0) 2021.09.24
[Python] 5174. subtree  (0) 2021.09.24
[Python] 1232. 사칙연산  (0) 2021.09.24