1. 문제
https://www.acmicpc.net/problem/11000
11000번: 강의실 배정
첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)
www.acmicpc.net
2. 접근방법
1. 시작시간 끝시간 각각 다른 pq에 저장
2. 시작에서 하나씩 꺼내서 class갯수 증가
3. 꺼낸 시작시간과 같거나 작은 끝시간이 있으면 모두 poll 하고 class 개수 감소
4. 갯수 증가 할 때마다 max 확인
5. 시작시간 pq에서 다 꺼내면 더 이상 class가 증가할 일 없으니 종료
6. max 출력
3. 자바 코드
package Gold;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class G5_11000강의실배정 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
PriorityQueue<Integer> start = new PriorityQueue<Integer>();
PriorityQueue<Integer> end = new PriorityQueue<Integer>();
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
start.add(Integer.parseInt(st.nextToken()));
end.add(Integer.parseInt(st.nextToken()));
}
int classs = 0;
int max = 0;
while (!start.isEmpty()) {
int s = start.poll();
while (end.peek() <= s) {
end.poll();
classs--;
}
max = Math.max(max, ++classs);
}
System.out.println(max);
}
}
4. 마치며
class가 변수명으로 못써서 classs로 했슴니다.
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준] 1726 로봇 (0) | 2021.11.20 |
---|---|
[백준] 9084 동전 (0) | 2021.11.09 |
[백준] 2357 최솟값과 최댓값 (0) | 2021.11.02 |
[백준] 2250 트리의 높이와 너비 (0) | 2021.11.01 |
[백준] 2263 트리의 순회 (0) | 2021.11.01 |