[BOJ] 1715 카드 정렬하기

nerry·2022년 1월 20일
0

알고리즘

목록 보기
19/86

문제

me

import sys
import heapq
input = sys.stdin.readline
N=int(input())
cards=[]
for _ in range(N):
    heapq.heappush(cards,int(input())) # 최소 힙을 통해 정렬
time=0
while len(cards)!=1:
    compared=heapq.heappop(cards)+heapq.heappop(cards) # 가장 작은 두 개를 섞음
    time+=(compared)
    heapq.heappush(cards,compared) # 섞은 것을 heappush를 통해 다른 것과의 비교가 진행되도록
print(time)
# 항상 가장 작은 두 카드 묶음을 꺼내야 하니 새로 더한 것도 비교군에 넣어야 한다.

풀긴 풀었지만, 우선순위 큐를 이용해야 한다는 것을 이미 들어버려서 금방 풀었다.
어떤 연산을 한 뒤 새로 나온 것과 다른 기존 것들과의 비교가 필요하면 우선순위 큐를 사용해야 하는 것 같다.

profile
터벅터벅 개발(은좋은)자 로그

0개의 댓글