PS

[백준] 1931번 회의실 배정 (C++)

-minari- 2024. 10. 16. 11:28

https://www.acmicpc.net/problem/1931

 

문제 설명

n개의 회의 시작 시간과 회의 끝나는 시간이 주어질 때, 최대로 사용할 수 있는 회의의 최대 개수를 출력하는 문제이다.

 

주요 로직

1. 정렬이다.

-> 회의가 끝나는 시간 기준으로 정렬을 해야한다.

 

정답 코드

#include <bits/stdc++.h>

using namespace std;

int n, s, e;
vector<pair<int,int>> v;

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_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> s >> e;
		v.push_back({s,e});
	}
	
	sort(v.begin(), v.end(), cmp);
	
	int cnt = 0;
	int time = 0;
	for(auto it : v){
		if(time <= it.first) time = it.second, cnt++;
	}
	
	cout << cnt << "\n";

	return 0;
}