[BOJ] 1913번 회의실 배정 - C++

"그리디 알고리즘"

Posted by Yongmin on June 14, 2021

문제

회의실 배정

풀이

끝나는 시간이 빠른 순으로 정렬한 뒤, iteration을 돌면서 끝나는 시간 뒤에 시작하는 가장 빠른 끝나는 시간을 갖는 회의를 진행하게 하며 count를 세는 것으로 정답을 도출할 수 있다.

소스 코드

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(pair<int, int> a, pair<int, int> b){
    if(a.second < b.second){
        return true;
    }
    else if(a.second == b.second){
        if(a.first < b.first){
            return true;
        }
    }
    return false;
}

int main(){
    int N;
    scanf("%d", &N);
    
    vector<pair<int, int>> order;
    
    for(int i = 0; i<N; i++){
        int input1;
        int input2;
        scanf("%d %d", &input1, &input2);
        order.push_back(make_pair(input1, input2));
    }
    
    sort(order.begin(), order.end(), compare);
    
    int start = 0;
    int cnt = 0;
    for(int i = 0; i<N; i++){
        if(start <= order[i].first){
            cnt++;
            start = order[i].second;
        }
    }
    
    printf("%d\n", cnt);
    
}




# # #