※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 5353번 문제인 Open Intervals이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/5353
5353번: Open Intervals
Given n open intervals (a1, b1), (a2, b2), ..., (an, bn) on the number line, each representing start and end times of some activity requiring the same resource, the goal is to find the largest number of these intervals so that no two of them overlap.
www.acmicpc.net
모든 구간 [L,R]들에 대하여 L 오름차순으로 각 구간을 살펴 문제를 해결할 수 있다.
\(R \le L\)인 구간들은 모두 공집합과 같으므로 다른 모든 구간과 겹치지 않는 것으로 계산하자.
또한 두번째 예제에서 관찰할 수 있듯이 (A,B)와 (B,C) (단, A<B<C)는 겹치지 않는 두 구간임에 유의해 구현하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
while (N) {
int ans = 0;
priority_queue<pair<int, int>> pq;
while (N--) {
int L, R; cin >> L >> R;
if (L >= R) ans++;
else pq.push({ -L,-R });
}
while (!pq.empty()) {
int L = -pq.top().first, R = pq.top().second; pq.pop();
if (R < 0) pq.push({ R,ans + 1 });
else ans = max(ans, R);
}
cout << ans << '\n';
cin >> N;
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 26736 // C++] Wynik meczu (0) | 2022.12.23 |
---|---|
[BOJ 26553 // C++] Work (0) | 2022.12.23 |
[BOJ 21771 // C++] 가희야 거기서 자는 거 아니야 (0) | 2022.12.23 |
[BOJ 12791 // C++] Starman (0) | 2022.12.23 |
[BOJ 5345 // C++] PLU Count (0) | 2022.12.22 |