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

최대 1 분 소요

1.문제 링크

회의실배정



2. 풀이 전 계획과 생각

  • 그리디



3. 풀이

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

// 종료시간을 기준으로 정렬 
bool cmp(const pair<int,int> &u, const pair<int,int> &v) {
    if (u.second == v.second) {
        return u.first < v.first;
    } else {
        return u.second < v.second;
    }
}

int main(){
	int n; cin>>n;
	
	vector<pair<int,int> > schedule;
	
	for(int i=0;i<n;i++){
		int start, end;
		cin>>start>>end;
		schedule.push_back({start,end});
	}
	
	// 정렬
	sort(schedule.begin(), schedule.end(), cmp);
	
	int ans=0;
	int now=0;
	for(int i=0;i<schedule.size();i++){
		if(now<=schedule[i].first){
			now = schedule[i].second;
			ans++;
		}
	}
	
	// 답 출력 
	cout<<ans; 
	
}



4. 풀이하면서 고민했던 점

  • 무엇을 기준으로 정렬할지 고민



5. 문제를 풀고 알게된 개념 및 소감