1+ /*
2+ Problem: Room Allocation
3+ Link: https://cses.fi/problemset/task/1164
4+ Author: Krishna Sikheriya (Krishna200608)
5+
6+ Short Problem Statement:
7+ There is a hotel and n customers with specific arrival and departure times.
8+ We need to assign rooms to customers such that the total number of rooms used is minimized.
9+ Two customers can stay in the same room if the first customer leaves before the second arrives.
10+ Print the minimum number of rooms needed and the room assignment for each customer.
11+
12+ Approach:
13+ 1. Store customer details (arrival, departure, original_index).
14+ 2. Sort customers based on arrival times.
15+ 3. Use a Min-Priority Queue (Min-Heap) to store pairs of {departure_time, room_number} for currently occupied rooms.
16+ The heap allows us to efficiently find the room that becomes free earliest.
17+ 4. Iterate through the sorted customers:
18+ - If the earliest departure time in the heap is less than the current customer's arrival time,
19+ it means that room is free. We reuse that room (pop from heap).
20+ - If no room is free, we allocate a new room number.
21+ - Push the current customer's departure time and assigned room number into the heap.
22+ - Store the assigned room number in an answer array using the original index.
23+ 5. The maximum size of the heap reached (or the max room ID generated) gives the minimum rooms required.
24+
25+ Time Complexity: O(N log N) - dominated by sorting the customers. Heap operations take O(log N).
26+ Space Complexity: O(N) - to store customer details, the priority queue, and the answer array.
27+
28+ Example I/O:
29+ Input:
30+ 3
31+ 1 2
32+ 2 4
33+ 4 4
34+
35+ Output:
36+ 2
37+ 1 2 1
38+ */
39+
40+ #include < iostream>
41+ #include < vector>
42+ #include < algorithm>
43+ #include < queue>
44+
45+ using namespace std ;
46+
47+ // Struct to keep track of customer details and their original order
48+ struct Customer {
49+ int id;
50+ int arrival;
51+ int departure;
52+ };
53+
54+ // Comparator to sort customers by arrival time
55+ bool compareCustomers (const Customer& a, const Customer& b) {
56+ return a.arrival < b.arrival ;
57+ }
58+
59+ int main () {
60+ // Fast I/O
61+ ios_base::sync_with_stdio (false );
62+ cin.tie (NULL );
63+
64+ int n;
65+ if (!(cin >> n)) return 0 ;
66+
67+ vector<Customer> customers (n);
68+ for (int i = 0 ; i < n; ++i) {
69+ customers[i].id = i;
70+ cin >> customers[i].arrival >> customers[i].departure ;
71+ }
72+
73+ sort (customers.begin (), customers.end (), compareCustomers);
74+
75+ // Min-priority queue to store {departure_time, room_number}
76+ // We want the room that frees up earliest to be at the top
77+ priority_queue<pair<int , int >, vector<pair<int , int >>, greater<pair<int , int >>> pq;
78+
79+ vector<int > ans (n);
80+ int last_room_id = 0 ;
81+
82+ for (int i = 0 ; i < n; ++i) {
83+ int assigned_room;
84+
85+ // Check if the earliest departing room is free before current arrival
86+ if (!pq.empty () && pq.top ().first < customers[i].arrival ) {
87+ assigned_room = pq.top ().second ;
88+ pq.pop ();
89+ } else {
90+ // Allocate a new room
91+ last_room_id++;
92+ assigned_room = last_room_id;
93+ }
94+
95+ // Push current customer's departure and room info
96+ pq.push ({customers[i].departure , assigned_room});
97+ ans[customers[i].id ] = assigned_room;
98+ }
99+
100+ // Output results
101+ cout << last_room_id << endl;
102+ for (int i = 0 ; i < n; ++i) {
103+ cout << ans[i] << " " ;
104+ }
105+ cout << endl;
106+
107+ return 0 ;
108+ }
109+
110+ /*
111+ SUBMISSION LINK:
112+ https://cses.fi/paste/d01d5f0ab0c6ae25f11225/
113+ */
0 commit comments