https://www.acmicpc.net/problem/1931
1931번: 회의실 배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
이 문제는 vector pair와 sort를 사용하여
회의가 끝나는 시간을 오름차순으로 정렬했다.
그리고 회의가 끝나는 시간을 기준으로
회의 시작 시각과 비교하여 조건문을 작성하였다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.second == b.second)
return a.first < b.first;
return a.second < b.second;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, start, end;
cin >> n;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
cin >> start >> end;
v.push_back(make_pair(start, end));
}
sort(v.begin(), v.end(), cmp);
int cnt = 1;
int endTime = v[0].second;
for (int i = 1; i < n; i++) {
if (endTime <= v[i].first) {
endTime = v[i].second;
cnt++;
}
}
cout << cnt;
return 0;
}
'코딩테스트 > Baekjoon' 카테고리의 다른 글
[백준 #10809] 알파벳 찾기 (C++) (0) | 2021.09.01 |
---|---|
[백준 #1152] 단어의 개수 (C++) (0) | 2021.09.01 |
[백준 #9655] 돌 게임 (C++) (0) | 2021.08.29 |
[백준 #2193] 이친수 (C++) (0) | 2021.08.28 |
[백준 #2217] 로프 (C++) (0) | 2021.08.27 |