반응형

https://www.acmicpc.net/problem/11000

 

11000번: 강의실 배정

첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. priority_queue를 만들고, 시간을 기준으로 오름차순으로 정렬하되 시간이 같으면 끝나는 시간이 앞으로 오도록 우선순위를 정합니다.

 

2. pq에서 값을 하나씩 꺼내면서 temp에 시작하는 시간이면 1을 더하고, 끝나는 시간이면 1을 빼줍니다.

 

3. temp의 값 중 가장 큰 값을 ans에 저장합니다.

 

4. ans를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<queue>
 
using namespace std;
 
int N;
 
struct Class {
    char cur;
    int time;
};
 
struct cmp {
    bool operator()(Class right, Class left) {
        if (right.time == left.time) {
            if (left.cur == 'e') {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return left.time < right.time;
        }
    }
};
 
int main()
{
    priority_queue<Class, vector<Class>, cmp> pq;
 
    scanf("%d"&N);
 
    for (int i = 0; i < N; i++) {
        int S, T;
        scanf("%d %d"&S, &T);
 
        pq.push({ 's', S});
        pq.push({ 'e', T});
    }
 
    int ans = 0;
    int temp = 0;
 
    while (!pq.empty()) {
        const char cur = pq.top().cur;
        pq.pop();
 
        if (cur == 's') temp++;
        else temp--;
 
        ans = max(ans, temp);
    }
 
    printf("%d", ans);
}
cs
반응형

+ Recent posts