-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom_Allocation.cpp
More file actions
56 lines (48 loc) · 1.31 KB
/
Copy pathRoom_Allocation.cpp
File metadata and controls
56 lines (48 loc) · 1.31 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
#define forn(i, k, e) for (int i = k; i < e; i++)
typedef pair<int, int> pi;
/*
sort based on entry times
put residents in buckets
for each new resident find a bucket whose exit time is less than current entry time
set the bucket exit time = current exit time
store bucket exit times in min_heap
*/
int main() {
int n;
cin >> n;
vector<tuple<int, int, int>> v;
forn(i, 0, n) {
int s, e;
cin >> s >> e;
v.push_back({s, e, i});
}
sort(v.begin(), v.end(), [](auto x, auto y) {
auto [s1, e1, i] = x;
auto [s2, e2, j] = y;
if (s1 != s2)
return s1 < s2;
return e1 > e2;
});
auto comp = [](pi x, pi y) { return x.first >= y.first; };
priority_queue<pi, vector<pi>, decltype(comp)> pq(comp);
map<int, vector<int>> buckets;
vector<int> rooms(n, 0);
for (auto [s, e, i] : v) {
if (pq.size() > 0 && pq.top().first < s) {
int room = pq.top().second;
rooms[i] = room;
pq.pop();
pq.push({e, room});
} else {
int room = pq.size() + 1;
rooms[i] = room;
pq.push({e, room});
}
}
cout << pq.size() << endl;
for (int room : rooms)
cout << room << " ";
return 0;
}