solution
1. 카드 묶음 배열 정렬
# 1715, 카드 정렬하기
import sys
n = int(sys.stdin.readline())
ns = []
for _ in range(n):
ns.append(int(sys.stdin.readline()))
ns.sort()
temp = 0
for i in range(len(ns)-1):
temp += ns[i] + ns[i+1]
ns[i+1] = temp
print(temp)
메모리 초과
2. 힙 이용
# 1715, 카드 정렬하기
import sys
import heapq
n = int(sys.stdin.readline())
ns = []
for _ in range(n):
heapq.heappush(ns, int(sys.stdin.readline()))
if len(ns) == 1:
print(0)
else:
ans = 0
while len(ns) > 1:
temp1 = heapq.heappop(ns)
temp2 = heapq.heappop(ns)
ans += temp1 + temp2
heapq.heappush(ns, temp1 + temp2)
print(ans)
32556KB
248ms
문제 출처 www.acmicpc.net/problem/1715
1715번: 카드 정렬하기
정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장��
www.acmicpc.net
'알고리즘 > 우선순위 큐' 카테고리의 다른 글
자바 | 백준 | 17612 | 쇼핑몰 (0) | 2021.05.01 |
---|---|
파이썬 | 백준 | 1931 | 회의실배정 | 런타임에러 (0) | 2020.11.11 |