886. Assigning the Lowest Free Seat
A hall has m seats numbered 1 to m, all free to begin with. A booking
system processes q operations:
reserve give out the LOWEST-numbered free seat and print it
release x seat x becomes free again
Every reserve is guaranteed to have at least one free seat, and every
release x names a seat that is currently taken.
m = 10^9: reserve, reserve, release 1, reserve, reserve -> 1, 2, 1, 3
m = 3: reserve, reserve, reserve -> 1, 2, 3
m = 5: reserve, release 1, reserve, release 1 -> 1, 1
In the first row seats 1 and 2 go out, then seat 1 comes back and is
immediately the lowest free seat again, so the next reserve hands it out
before seat 3.
## The constraint that decides the method
m is up to 10^9 and q is only up to 200000.
That single line rules out the obvious solution. Putting every seat number into a min-heap up front would be a billion pushes and about eight gigabytes of memory for the array alone. A boolean array of a billion seats is no better. Whatever the structure is, it has to be sized by the number of OPERATIONS, not by the number of seats.
This is worth noticing as a habit. When one bound is enormous and another is small, the algorithm has to be paid for out of the small one.
## Two kinds of free seat
Split the free seats into two groups that are stored completely differently:
seats that have NEVER been handed out -- a single counter next
seats that were handed out and RELEASED -- a min-heap
The never-used seats are next, next + 1, next + 2, ... up to m, which is a
range, and the smallest of them is next. A range needs one integer, not a
data structure.
The released seats are scattered, so they need the heap, and there are at most
q of them.
next = 1, h = empty min-heap
reserve:
if h is non-empty: print pop(h)
else: print next, next = next + 1
release x:
push x into h
## Why the heap always wins when it is non-empty
Every seat in the heap was handed out at some earlier moment, and seats are
only ever handed out with a number below next. So every released seat is
strictly less than next, and if the heap holds anything at all, its minimum
beats next. No comparison between the two groups is needed, which is a
pleasant consequence of the numbering rather than an accident.
That argument is the entire correctness proof, and it is the reason the code is three lines instead of ten.
## The boundary that breaks a careless implementation
Handing out next and forgetting to increment it, which gives the same seat
to two bookings. The test that catches this is a run of plain reserves with no
releases at all: 1 2 3 rather than 1 1 1.
The second boundary is a release of a seat that is then immediately re-reserved several times over, as in the third example. A structure that tracks only "how many are free" cannot answer this, because the identity of the seat matters.
## What is NOT an overflow here
Seat numbers reach 10^9, which fits in a signed 32-bit integer since
2147483647 is larger. Nothing is summed. The trap in this problem is memory,
not arithmetic, and confusing the two leads to widening types that were never
in danger while still allocating a billion of them.
## Output when nothing is asked
A log made only of releases produces no lines. Print none in that case.
## Related problems
Problem 872 is the same min-heap with no bounded universe and no counter, so
comparing the two shows exactly what the next counter buys. Problem 891 also
keeps a heap that must cope with values leaving, but there the departures are
arbitrary and the trick is lazy deletion rather than a range.
## Cost
Time O(q log q). Space O(q), independent of m.
Constraints - `1 ≤ m ≤ 1000000000` - `1 ≤ q ≤ 200000` - `1 ≤ x ≤ m` - Every `reserve` has a free seat available, and every `release x` names a seat that is currently taken.
Input
The first line contains two integers m and q.
Each of the next q lines is either the single word reserve, or release x with an integer seat number.
Output
Print one line for every reserve operation, in order: the number of the seat handed out. If the log contains no reserve at all, print the single word none.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.