[ ALGORITHM ]/[ 백 준 ]

[BAEKJOON] 1931번 회의실배정

HiStar__ 2020. 7. 7. 21:00

문 제

 

소 스  코 드

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

int main() {
	int N;

	std::cin >> N;

	std::vector<std::pair<int, int>> conference;

	conference.reserve(100'000);

	int start, end;
	for (int i = 0; i < N; ++i) {
		std::cin >> start >> end;
		conference.emplace_back(std::make_pair(start, end));
	}


	std::sort(
		conference.begin(), conference.end(), 
		[](const std::pair<int, int>& left, const std::pair<int, int>& right) {
		if (left.second == right.second) return left.first < right.first;
		else return left.second < right.second; 
	}
	);

	int temp{ conference[0].second };
	int count{ 1 };


	for (int i = 1; i < N; ++i) {
		if (conference[i].first >= temp) {
			temp = conference[i].second;
			++count;
		}
	}
	std::cout << count;

	return 0;
}

 

풀 이

 

  • 끝나는 시간대로 정렬을 할 경우 
    13 12 | 11 12 일 경우 끝나는 시간대로 정렬 일 경우 13 12 | 11 12로 정렬 된다.

  • 끝나는 시간이 같다면 시작 시간으로 정렬 아닐 경우 끝나는 시간으로 정렬
    13 12 | 11 12 일 경우 11 12 | 13 12 로 정렬 된다.
  • 끝나는 시간으로 정렬 해서 구현시 뒤에 자리도 생각을 해서 구현을 해야한다. 조건문 추가.
    끝나는 시간이 같다면 시작 시간으로 정렬 아닐 경우 끝나는 시간으로 정렬할 경우
    앞자리만 비교하여 진행하면 쉽게 구현이 가능하다.

 

출 력 값

 

 

문 제  출 처

문제 링크 : [ BAEKJOON ] : https://www.acmicpc.net/problem/1931