알고리즘/백준
백준 11000 강의실 배정 Java [우선순위 큐]
쫑쫑2
2022. 11. 27. 00:09
https://www.acmicpc.net/problem/11000
11000번: 강의실 배정
첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)
www.acmicpc.net
우선순위 큐를 2개 만들어 한 우선순위큐는 예제의 첫번째 값을 오름차순으로 하여 만들고
이 큐의 size가 0이 될 때 까지 반복문을 돌려
큐의 peek값보다 작거나 같은 끝나는 시간 값 큐에 있는 값들을 모두 빼고, 강의실 카운트에서 뺀다
하나씩 꺼내고 끝나는 시간 값을 다른 우선순위큐에 오름차순으로 하여 넣고,
강의실 카운트를 더한 후 강의실 최대값과 비교하여 갱신한다
import java.awt.*;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Queue<Point> queue = new PriorityQueue<>(((o1, o2) -> o1.x > o2.x ? 1 : -1));
Queue<Integer> end = new PriorityQueue<>();
for (int n = 0; n < N; n++) {
StringTokenizer st = new StringTokenizer(br.readLine());
queue.add(new Point(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
int result = 0, room = 0;
while (!queue.isEmpty()) {
while (!end.isEmpty() && queue.peek().x >= end.peek()) {
end.poll();
room--;
}
end.add(queue.poll().y);
room++;
result = Math.max(result, room);
}
System.out.println(result);
}
}