aboutsummaryrefslogtreecommitdiff
path: root/02_sorting_and_searching/room_allocation_1164.cpp
blob: f79cc96442656b74da410f1bb94748d35425d139 (plain)
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
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

int main() {
	int n;
	std::vector<std::tuple<int, int, int>> v;
	std::cin >> n;
	for (int i = 0; i < n; i++) {
		int a, b;
		std::cin >> a >> b;
		v.push_back({a, b, i});
	}
	std::sort(v.begin(), v.end());

	using P = std::pair<int, int>;
	std::priority_queue<P, std::vector<P>, std::greater<>> r;
	std::vector<int> al(n);
	for (auto [a, d, i] : v) {
		int ind = r.size()+1;
		if (!r.empty()) {
			auto [x, j] = r.top();
			if (a > x) {
				r.pop();
				ind = j;
			}
		}
		r.push({d, ind});
		al[i] = ind;
	}

	std::cout << r.size() << "\n";
	for (auto i : al)
		std::cout << i << " ";
	std::cout << "\n";
}

Generated with cgit - Back to sebastiano.tronto.net