1931번 - 회의실 배정
그리디 알고리즘에서 가장 대표적인 문제.
가장 빨리 끝나는 회의를 가장 먼저 시작하면 된다.
##알게된 점##
vector<pair<int,int>> 가 sort 함수를 썼을 때 어떻게 될지 궁금했는데,
first에 대해서 오름차순, 그리고 같은 first 값은 second 값에 따라 오름차순으로 정렬이 됐다. (뭐 당연한 얘기지만 확인 사살)
처음에 v.resize(n+1)을 해줘서 정렬할 때, 입력하지 않은 수 (0,0) 까지 정렬되었다... 이런거 조심하자.
<정답 코드>
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 | #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n, ans = 0; vector<pair<long long, long long>> v; scanf("%d", &n); v.resize(n); for (int i = 0; i < n; i++) { scanf("%lld", &v[i].second); scanf("%lld", &v[i].first); } sort(v.begin(), v.end()); long long endtime = 0; for (int i = 0; i < n; i++) { if (endtime <= v[i].second) { endtime = v[i].first; ans++; } } printf("%d\n", ans); return 0; } | cs |
반응형