acmicpc
[BAEKJOON] 1715. 카드 정렬하기
코드와이
2021. 8. 22. 17:07
문제링크
https://www.acmicpc.net/problem/1715
1715번: 카드 정렬하기
정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장
www.acmicpc.net
PriorityQueue 를 사용한다면 쉽게 해결할 수 있는 문제
100%에서 틀린다면 묶음이 하나 있는 경우 => 비교횟수 = 0
나는 묶음이 하나 있다면 그 묶음의 수를 return 해서 틀렸다.
package acmicpc.Gold4;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class 카드_정렬하기 {
// 가장 작은 수의 묶음들을 먼저 비교하면 된다.
static int n, ans;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0 ; i < n ; i++) {
pq.add(Integer.parseInt(br.readLine()));
}
ans = 0;
while(pq.size() != 1) {
int a = pq.poll();
int b = pq.poll();
ans += (a + b);
pq.add(a + b);
}
System.out.println(ans);
}
}