pq > 5 4 3 2 1
offer > (자동정렬) > remove
-
while 내부 local pointer
- (pq.size != 1) 로 표현해도 되고
- (pq.size() > 1)로 표혀해도 됨
- 중요한거는 2가 빠지고 1이 추가되므로 1씩 줄어든다
plate는 이제까지의 횟수가 모두 누적된 것이므로,
새로 넣는 offer는 plate가 아닌 d1+d2를 넣어야 함
-
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// ISC
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
// D2F
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 1; i <= N; i++) {
int p = sc.nextInt();
pq.add(p);
}
// OP
int plate = 0;
while (pq.size() > 1) {
int d1 = pq.remove();
int d2 = pq.remove();
plate += (d1 + d2);
pq.offer(d1 + d2);
}
// OEC
System.out.println(plate);
}
}
'Hard deck > reindexing d1' 카테고리의 다른 글
036 : 최솟값을 만드는 괄호 배치 찾기 (1) | 2023.06.13 |
---|---|
034 : 수를 묶어서 최댓값 만들기 (2) | 2023.06.13 |
032 : 동전 개수의 최솟값 구하기 (2) | 2023.06.13 |
043 : 최대 공약수 구하기 (1) | 2023.06.13 |
042 : 최소 공배수 구하기 (0) | 2023.06.13 |