백준 1931 c++ ❌

CJB_ny·2023년 2월 22일
0

백준

목록 보기
85/104
post-thumbnail

회의실 배정

내풀이

시간초과 뜸.

vector<pair<int, int> vec;

sort(vec.begin(), vec.end()); 할경우

first를 기준으로 greater, second를 기준으로 greater 오름차순 순으로 정렬한다.

이문제같은 경우 나는 이중반복문으로 dp를 썻는데 시간초과가 났고

시작시간과 끝시간을 위치를 바꾸고 정렬을 해준다음에 시간이 곂치는지 안곂치는지만 확인을 바로해줌.

끝시간을 first에 넣고 정렬하면 가장 빨리 끝나는 시간을 찾을 수 있다는 점이 핵심인듯

cpp 코드

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <algorithm>
using namespace std;
#define endl "\n"
#define ll long long
#define MAX 100004

int n, Start, End, idx = 0;
int ret = 1;
int from, to;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	vector<pair<int, int>> v;
	for (int i = 0; i < n; ++i)
	{
		cin >> from >> to;
		v.push_back({to, from});
	}
	sort(v.begin(), v.end());
	from = v[0].second;
	to = v[0].first;
	for (int i = 1; i < n; ++i)
	{
		if (v[i].second < to) continue;
		from = v[i].second; to = v[i].first; ++ret;
	}
	cout << ret;

	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글