반응형
https://www.acmicpc.net/problem/1931
1931번: 회의실배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
끝나는 시간을 기준으로 sort한다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Pos
{
int start, end;
};
bool compare(Pos a, Pos b)
{
if (a.end != b.end)
{
return a.end < b.end;
}
else
return a.start < b.start;
}
int main()
{
int n, in_str, in_end, cnt = 0 , tmp = 0;
cin >> n;
vector<Pos> arr(n);
for (int i = 0; i < n; i++)
{
cin >> in_str >> in_end;
arr[i].start = in_str;
arr[i].end = in_end;
};
sort(arr.begin(), arr.end(), compare);
for (int i = 0; i < arr.size(); i++)
{
if(tmp<=arr[i].start)
{
tmp = arr[i].end;
cnt++;
}
}
cout << cnt << endl;
}
반응형
'BOJ > greedy' 카테고리의 다른 글
백준 2217번 : 로프 (0) | 2019.08.07 |
---|---|
백준 1541 : 잃어버린 괄호 (0) | 2019.08.06 |
백준 11047 : 동전 0 (0) | 2019.08.05 |
백준 11399 : ATM (0) | 2019.08.05 |