BOJ 1781 컵라면
BOJ 1781 컵라면
https://www.acmicpc.net/problem/1781
문제
상욱 조교는 동호에게 N개의 문제를 주고서, 각각의 문제를 풀었을 때 컵라면을 몇 개 줄 것인지 제시 하였다. 하지만 동호의 찌를듯한 자신감에 소심한 상욱 조교는 각각의 문제에 대해 데드라인을 정하였다.
문제 번호 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
데드 라인 | 1 | 1 | 3 | 3 | 2 | 2 | 6 |
컵라면 수 | 6 | 7 | 2 | 1 | 4 | 5 | 1 |
위와 같은 상황에서 동호가 2, 6, 3, 1, 7, 5, 4 순으로 숙제를 한다면 2, 6, 3, 7번 문제를 시간 내에 풀어 총 15개의 컵라면을 받을 수 있다.
문제는 동호가 받을 수 있는 최대 컵라면 수를 구하는 것이다. 위의 예에서는 15가 최대이다.
문제를 푸는데는 단위 시간 1이 걸리며, 각 문제의 데드라인은 N이하의 자연수이다. 또, 각 문제를 풀 때 받을 수 있는 컵라면 수와 최대로 받을 수 있는 컵라면 수는 모두 $2^{31}$보다 작은 자연수이다.
입력
첫 줄에 숙제의 개수 N (1 ≤ N ≤ 200,000)이 들어온다. 다음 줄부터 N+1번째 줄까지 i+1번째 줄에 i번째 문제에 대한 데드라인과 풀면 받을 수 있는 컵라면 수가 공백으로 구분되어 입력된다.
출력
첫 줄에 동호가 받을 수 있는 최대 컵라면 수를 출력한다.
예제 입력 1
7 1 6 1 7 3 2 3 1 2 4 2 5 6 1
예제 출력 1
15
코드
BOJ 2109 순회강연 문제와 완전히 같은 문제이다.
자세한 주석은 생략
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Main {
static class Problem implements Comparable<Problem> {
int deadline, cupNoodle;
public Problem(int deadline, int cupNoodle) {
this.deadline = deadline;
this.cupNoodle = cupNoodle;
}
@Override
public int compareTo(Problem o) {
return deadline - o.deadline;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PriorityQueue<Problem> input = new PriorityQueue<>();
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
input.offer(new Problem(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
PriorityQueue<Integer> selected = new PriorityQueue<>();
while (!input.isEmpty()) {
Problem check = input.poll();
if(check.deadline > selected.size())
selected.offer(check.cupNoodle);
else if(check.cupNoodle > selected.peek()) {
selected.poll();
selected.offer(check.cupNoodle);
}
}
int answer = 0;
while (!selected.isEmpty()) answer += selected.poll();
System.out.println(answer);
}
}
This post is licensed under CC BY 4.0 by the author.