본문 바로가기
알고리즘/그리디 알고리즘

(복습필요) 11000 강의실 배정

by tryotto 2019. 2. 19.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <queue>
#include <utility>
#include <functional>
#include <vector>
#include <algorithm>
 
typedef long long ll;
using namespace std;
priority_queue<ll, vector<ll>, greater<ll> > pq;
vector<pair<ll, ll> > arr(300000);
 
bool compare(const pair<ll, ll>& a, const pair<ll, ll>& b) {
    if (a.first == b.first) {
        return a.second < b.second;
    }
    return a.first < b.first;
}
 
int main() {
    int n;
    scanf("%d"&n);
 
    for (int i = 1; i <= n; i++) {
        scanf("%lld %lld"&arr[i].first, &arr[i].second);
    }
    sort(&arr[1], &arr[n+1], compare);
 
    pq.push(arr[1].second);
    int tmp = 0;
    for (int i = 2; i <= n; i++) {        
        ll start = arr[i].first;
        ll end = arr[i].second;        
        if (pq.top() <= start) {
            pq.push(end);
            pq.pop();        
        }
        else {
            pq.push(end);            
        }
    }
    printf("%d", pq.size());
}
cs


'알고리즘 > 그리디 알고리즘' 카테고리의 다른 글

knapsack 더블릿  (0) 2019.02.20
mixing milk 더블릿  (0) 2019.02.20
1931 회의실 배정  (0) 2019.02.19
11047 동전만들기  (0) 2019.02.19
1449 수리공 항승  (0) 2019.02.19