https://www.acmicpc.net/problem/2109
2109번: 순회강연
한 저명한 학자에게 n(0 ≤ n ≤ 10,000)개의 대학에서 강연 요청을 해 왔다. 각 대학에서는 d(1 ≤ d ≤ 10,000)일 안에 와서 강연을 해 주면 p(1 ≤ p ≤ 10,000)만큼의 강연료를 지불하겠다고 알려왔다.
www.acmicpc.net
저는 우선순위 큐를 통해 해당 문제를 해결하였습니다.
시간은 역순으로 최대 일 수 부터 시작하여 가장 높은 가격이 먼저 나오는 우선순위 큐를 만들었습니다.
package BOJ.greedy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_2109 {
static class Class{
int price;
int duration;
public Class(int price, int duration) {
this.price = price;
this.duration = duration;
}
}
private static final int INF = 987654321;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
Class[] classes = new Class[n];
int maxDuration = 0;
for(int i = 0 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
int price = stoi.apply(st.nextToken());
int duration = stoi.apply(st.nextToken());
maxDuration = Math.max(duration,maxDuration);
classes[i] = new Class(price,duration);
}
Arrays.sort(classes,(o1,o2)->{ // 내림차순 정렬
return o2.duration - o1.duration;
});
PriorityQueue<Class> pq = new PriorityQueue<>((o1,o2)->{ // 내림차순 정렬
return o2.price - o1.price;
});
int index = 0;
int result = 0;
for(int time = maxDuration ; time >= 1 ; time--){
while(index < n && classes[index].duration == time){
pq.offer(classes[index++]);
}
if(!pq.isEmpty()){
result += pq.poll().price;
}
}
System.out.println(result);
}
}
'알고리즘' 카테고리의 다른 글
백준 19638번 센티와 마법의 뿅망치 (JAVA) (0) | 2022.07.20 |
---|---|
백준 2613번 숫자구슬 (0) | 2022.07.19 |
백준 14496번 그대, 그머가 되어 (JAVA) (0) | 2022.07.17 |
프로그래머스 네트워크 (JAVA) (0) | 2022.07.16 |
프로그래머스 문자열 내림차순으로 배치하기 (JAVA) (0) | 2022.07.15 |