코딩테스트/백준
[BOJ 1715] 카드 정렬하기(G4)
34suuuuu
2024. 11. 27. 10:43
📍 문제
https://www.acmicpc.net/problem/1715
📍 문제 풀이
데이터를 정렬 -> 조회 -> 삽입이 반복적으로 이루어진다
우선순위 큐 사용
1. 데이터 전체를 오름차순으로 정렬
2. 가장 작은 숫자를 더해서 다시 우선순위 큐에 삽입
3. 우선순위 큐의 사이즈가 1이 될 때까지 (1)(2)과정 반복
📍 전체 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
public class boj_1715 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
pq.add(Integer.parseInt(br.readLine()));
}
int result = 0;
while (pq.size() > 1) {
int first = pq.poll();
int second = pq.poll();
result += (first + second);
pq.add(first + second);
}
System.out.println(result);
}
}
..우선순위 큐에 이상한 값 넣다가 틀렸습니다 💣폭탄맞기..