david의 CS Blog 자세히보기

백준 문제 풀이

BOJ 11000 강의실 배정

david0506 2021. 2. 11. 10:59

greedy로 회의 정렬 + 우선순위 큐를 이용해 강의실 개수 관리

 

우선순위 큐에 들어있는 원소를 $i$번째 회의실을 마지막에 사용하는 시간이라고 정의하고 i번째 회의실에서 회의를 이어서 할 수 있다면 그 값을 바꾼다.

 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct info{
    int s, e;
};
int n;
vector<info> v;
bool cmp(const info &a, const info &b)
{
    return a.s==b.s ? a.e<b.e : a.s<b.s;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i=0; i<n; i++){
        int a, b; cin >> a >> b;
        v.push_back({a, b});
    }
    sort(v.begin(), v.end(), cmp);
    priority_queue<int> pq;
    pq.push(-v[0].e);
    for(int i=1; i<n; i++){
        if(-pq.top()<=v[i].s){
            pq.pop(); pq.push(-v[i].e);
        }
        else{ pq.push(-v[i].e); }
    }
    cout << pq.size() << "\n";
    return 0;
}