파이썬알고리즘
20210810#(76) 백준 1715번 카드 정렬하기
zho
2021. 8. 10. 19:03
https://www.acmicpc.net/problem/1715
1715번: 카드 정렬하기
정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장
www.acmicpc.net
처음으로 풀어본 골드 문제.. 골드 문제 중에서는 난이도가 낮은 문제인 것 같다.
다만 import heapq를 이용하는 우선순위 큐를 통해 풀수있기에 난이도가 좀 높게 책정된 것 같다.
○ 코드
import heapq
n=int(input())
cards=[]
for i in range(n):
heapq.heappush(cards,int(input()))
ans=0
while len(cards)>=2:
fir_num=heapq.heappop(cards)
sec_num=heapq.heappop(cards)
ans+=fir_num+sec_num
heapq.heappush(cards,fir_num+sec_num)
print(ans)
○ 배운 점
heapq 우선순위 큐 유형을 새롭게 알게 되었음.(heapq.heappush(card,int(input()), heapq.heappop(cards))
728x90