Home 백준 강의실 배정 (11000)
Post
Cancel

백준 강의실 배정 (11000)

백준 강의실 배정 (11000)

1

문제

수강신청의 마스터 김종혜 선생님에게 새로운 과제가 주어졌다.

김종혜 선생님한테는 Si에 시작해서 Ti에 끝나는 N개의 수업이 주어지는데, 최소의 강의실을 사용해서 모든 수업을 가능하게 해야 한다.

참고로, 수업이 끝난 직후에 다음 수업을 시작할 수 있다. (즉, Ti ≤ Sj 일 경우 i 수업과 j 수업은 같이 들을 수 있다.)

수강신청 대충한 게 찔리면, 선생님을 도와드리자!

입력

첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000)

이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)

출력

강의실의 개수를 출력하라.

문제 접근

  • 이른 시작 시간 순으로 클래스 배열을 오름 차순 정렬한다.
  • 정렬한 배열을 하나 씩 꺼내서 우선순위 큐에 넣는다. (종료 시간 빠른 우선순위 큐)
  • 우선 순위 큐 맨 앞 원소의 종료 시간과 배열안의 요소 시작 시간과 비교해 나간다.
  • 종료시간이 시작시간 보다 작으면 우선순위 큐에서 하나 제거하고 배열 안의 요소를 다시 넣어준다.
  • 위의 조건이 아니라면 우선순위 큐는 그대로 놔두고 배열 안의 요소를 큐에 넣는다.
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.*;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    public FastScanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }
}

class Room implements Comparable<Room>{
    int start;
    int end;

    public Room(int st, int end){
        this.start = st;
        this.end = end;
    }

    @Override
    public int compareTo(Room other){

        if(this.start == other.start) return (this.end >= other.end) ? 1 : -1;
        else return (this.start >= other.start) ? 1 : -1;
    }
}

public class Main {
    public static void main(String args[]) throws IOException{
        FastScanner sc = new FastScanner();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = sc.nextInt();

        Room [] arr = new Room[n];

        for(int i=0; i<n; i++)
            arr[i] = new Room(sc.nextInt(), sc.nextInt());

        Arrays.sort(arr);

        PriorityQueue<Integer> pq = new PriorityQueue<>();

        pq.add(arr[0].end);

        for(int i=1; i<n; i++){
            if(pq.peek() <= arr[i].start) {
                pq.add(arr[i].end);
                pq.poll();
            }else{
                pq.add(arr[i].end);
            }
        }

        bw.write(Integer.toString(pq.size()));

        bw.close();
    }

}

런타임 에러

1

시작 시간과 종료시간이 같을 경우를 고려 하지 않아서 위의 런타임 에러가 나왔다.

문제의 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Room implements Comparable<Room>{
    int st;
    int end;

    public Room(int st, int end){
        this.st = st;
        this.end = end;
    }

    @Override
    public int compareTo(Room other){
        return (this.st >= other.st) ? 1 : -1;
    }
}

수정 후 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Room implements Comparable<Room>{
    int start;
    int end;

    public Room(int st, int end){
        this.start = st;
        this.end = end;
    }

    @Override
    public int compareTo(Room other){

        if(this.start == other.start) return (this.end >= other.end) ? 1 : -1;
        else return (this.start >= other.start) ? 1 : -1;
    }
}

This post is licensed under CC BY 4.0 by the author.