884. Which Room Each Meeting Gets
An office books n meetings, each with a start time and an end time. Rooms
are numbered 0, 1, 2, ... and are created on demand.
Meetings are allocated in increasing order of start time. Ties are broken by the earlier end time, and then by the earlier position in the input. When a meeting starts, every room whose current meeting has ENDED is free again; a room that ends exactly when the meeting starts counts as free. The meeting takes the LOWEST-numbered free room, or a brand new room if none is free.
Print, for each meeting IN INPUT ORDER, the number of the room it got.
(0,30) (5,10) (15,20) -> 0 1 1
(1,2) (2,3) (3,4) -> 0 0 0
(0,5) (0,5) (0,5) (0,5) -> 0 1 2 3
(0,10) (1,2) (3,4) (5,6) -> 0 1 1 1
In the first row the long meeting takes room 0. The (5,10) meeting starts
while room 0 is busy, so room 1 is created. The (15,20) meeting starts after
room 1 freed at time 10, and room 0 is still busy until 30, so it reuses room
1.
The second row is the touching rule: (1,2) frees room 0 at time 2, and
(2,3) starts at time 2, so it may reuse room 0.
## What #277 does, and why it does not answer this
Problem 277 asks HOW MANY rooms are needed. Because the answer is only a count, the arrivals can be separated from the departures entirely: sort the two lists independently, sweep them together, and report the largest number in use at any moment. That sweep never knows which departure belongs to which arrival, and it does not need to.
Here the answer is WHICH room, so the pairing matters. A room becomes free because a SPECIFIC meeting ended, and the number it gives back is the number that meeting was holding. Decoupling the two lists throws away exactly the information the question asks for.
## Two heaps
``` busy a min-heap of (end time, room number) free a min-heap of room numbers made how many rooms exist
sort the meetings by (start, end, input index) for each meeting (s, e, i): while busy is non-empty and busy.top.end <= s: move that room number into free if free is non-empty: r = pop free else: r = made, made = made + 1 answer[i] = r push (e, r) into busy ```
The two heaps have different jobs and it is worth saying them out loud. The BUSY heap answers "which room frees up soonest", ordered by time. The FREE heap answers "which available room has the smallest number", ordered by room number. Using one heap for both, or ordering the free rooms by the time they were released, gives rooms out in the wrong order.
## Why the release loop is a while and not an if
Several rooms can free up before one meeting starts. In the fourth example the
meetings (1,2), (3,4) and (5,6) each release room 1 before the next one
claims it, but with a longer gap two or three rooms could be released at once.
An if releases only one of them and the next meeting opens a needless new
room.
## Why each room is released exactly once
A room enters busy once per meeting and leaves it once, so the total number
of pushes and pops is 2n however violently the loop swings on a single
meeting. That is the same amortised counting argument as a monotonic stack:
the inner while is unbounded per step and bounded in total.
## The boundary that breaks a careless implementation
The comparison busy.top.end <= s, with the =. Using < means a room
freed exactly at the start time is not reused, and the second example comes out
as 0 1 2 instead of 0 0 0. The statement fixes the convention; the code
has to match it.
The second one is the output order. The meetings are PROCESSED in start order and REPORTED in input order, so the answer has to be written into a slot indexed by the original position, not appended to a list.
## What is NOT an overflow here
Times reach 10^9, which fits in a signed 32-bit integer, and nothing is
added or multiplied. Room numbers are at most n.
## Related problems
Problem 277 counts the rooms with a sweep. Problem 1437 removes the fewest intervals so that none overlap, and problem 1036 packs the most meetings into one room; both are greedy sorts by end time with no heap at all. Problem 885 is the closest relative here: a fixed number of machines, each job going to whichever frees up soonest.
## Cost
Time O(n log n). Space O(n).
Constraints - `1 ≤ n ≤ 200000` - `0 ≤ s < e ≤ 1000000000`
Input
The first line contains one integer n.
Each of the next n lines contains two integers s and e, the start and end of one meeting.
Output
Print n integers on one line, separated by single spaces: the room number given to each meeting, in the order the meetings appear in the input. Rooms are numbered from 0. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.