https://www.acmicpc.net/problem/1781
1781번: 컵라면
상욱 조교는 동호에게 N개의 문제를 주고서, 각각의 문제를 풀었을 때 컵라면을 몇 개 줄 것인지 제시 하였다. 하지만 동호의 찌를듯한 자신감에 소심한 상욱 조교는 각각의 문제에 대해 데드라
www.acmicpc.net
문제
코드
import java.io.*;
import java.util.*;
public class Main {
private static final BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static class Node implements Comparable<Node> {
int deadLine;
int cup;
public Node(int deadLine, int cup) {
this.deadLine = deadLine;
this.cup = cup;
}
@Override
public int compareTo(Node node) {
if(this.deadLine < node.deadLine) return -1;
else if(this.deadLine == node.deadLine) {
if(this.cup > node.cup) return -1;
else if(this.cup == node.cup) return 0;
else return 1;
}
else return 1;
}
}
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(bf.readLine());
Node[] arr = new Node[n];
PriorityQueue<Integer> q = new PriorityQueue<>();
for(int i = 0 ; i < n ; i++) {
StringTokenizer st = new StringTokenizer(bf.readLine());
int deadLine = Integer.parseInt(st.nextToken());
int cup = Integer.parseInt(st.nextToken());
arr[i] = new Node(deadLine, cup);
}
Arrays.sort(arr);
for(Node a : arr) {
if(q.size() < a.deadLine) {
q.offer(a.cup);
} else if(q.size() == a.deadLine) {
int c = q.peek();
if(c < a.cup) {
q.poll();
q.offer(a.cup);
}
}
}
long sum = 0;
while(!q.isEmpty()) {
sum += q.poll();
}
bw.write(sum + "\n");
bf.close();
bw.flush();
bw.close();
}
}
데드라인과 컵라면의 개수를 Node로 만들어서 정렬하였습니다.
정렬의 우선 순위는 먼저, 데드라인이 짧아야하고, 두번째로는 컵라면의 개수입니다.
우선순위 큐를 이용해 큐에 컵라면의 개수를 넣어 최종적으로 남아있는 컵라면의 개수를 모두 더한 값을 출력합니다.
'코딩테스트' 카테고리의 다른 글
[백준] 5052번 '전화번호 목록' - Java (0) | 2023.08.19 |
---|---|
[백준] 1107번 '리모컨' - Java (1) | 2023.08.18 |
[백준] 1091번 '카드 섞기' - Java (1) | 2023.08.16 |
[백준] 1034번 '램프' - Java (0) | 2023.08.15 |
[백준] 7569번 '토마토' - Java (1) | 2023.08.13 |