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;
}
'백준 문제 풀이' 카테고리의 다른 글
BOJ 18251 내 생각에 A번인 단순 dfs 문제가 이 대회에서 E번이 되어버린 건에 관하여 (Easy) (0) | 2021.07.26 |
---|---|
[ dp ] LCS(Longest Common Substring) (0) | 2021.02.11 |