891. A Bag That Can Also Forget
Maintain a multiset of integers under q operations:
add v put another copy of v into the bag
remove v take one copy of v out; it is guaranteed to be there
max print the largest value in the bag, or empty if the bag is empty
add 5, add 9, max, remove 9, max, remove 5 -> 9, 5
add 1, remove 1, max -> empty
add 3, add 3, remove 3, max -> 3
The third row is the reason the bag is a MULTISET: two copies of 3 go in, one comes out, and one is still there.
## The thing a heap cannot do
A heap gives you the maximum in constant time and lets you remove THAT. It has
no way to remove a value from its middle: finding it is O(n), and even once
found, patching the hole is not a standard operation in any library heap.
So the problem is a direct attack on the structure's one weakness, and the answer is not to fix the heap but to work around it.
## What most people try first
Keeping a sorted array. max is free and both add and remove are
O(n) because of the shifting. At q = 200000 that is 2 * 10^10 moves.
Scanning the bag for the maximum. add and remove become cheap and
max becomes O(n), which is the same figure from the other side.
Rebuilding the heap after every removal. O(n) per removal, same again.
## Lazy deletion with a count of debts
Keep a max-heap that everything is pushed into and NOTHING is ever removed from except at the top. Alongside it keep a map from value to the number of copies that have been removed but are still sitting in the heap, and a plain counter for how many items the bag really holds.
add v: push v into the heap; size = size + 1
remove v: pending[v] = pending[v] + 1; size = size - 1
max: if size == 0: print "empty"
else:
while pending[top] > 0:
pending[top] = pending[top] - 1
pop
print top
## Why the cleanup loop always leaves a real value
When size > 0 the bag holds something, so the heap holds at least one entry
that is not owed to a removal. The loop only pops entries whose debt is
recorded, and it decrements that debt as it goes, so it can never pop more
copies of a value than were actually removed. It therefore stops at a genuine
member, which, being the largest surviving entry of the heap, is the bag's
maximum.
The separate size counter is what makes the empty case safe. Without it you
would have to decide emptiness from the heap, which is full of ghosts.
## Why it is fast
Each add pushes once. Each entry is popped at most once, ever. So the total
work across all the cleanup loops is bounded by the number of pushes, and the
whole run is O(q log q) even though one max can pop many entries. Again
the amortised argument, and again the individual step is unbounded.
## The boundary that breaks a careless implementation
Removing a value that is not currently at the top. This is the normal case, and it is why the debt is recorded rather than acted on. A program that searches the heap array and swaps the value to the end destroys the heap property for everything above it.
Duplicates. pending must count copies, not mark a value as deleted. With
a set instead of a counter, add 3, add 3, remove 3, max answers empty or
crashes instead of answering 3.
Deciding emptiness from the heap. After add 1, remove 1, the heap still
holds the 1 and the bag is empty. Only the counter knows.
## What is NOT an overflow here
Values reach 10^9 in magnitude, inside the 32-bit range whose top is
2147483647, and nothing is summed. The counter is at most q.
## Related problems
Problem 890 also uses lazy deletion, but there staleness is decided by a
monotone clock, so a single comparison identifies a dead entry and no bookkeeping
is needed. Problem 192 reports the maximum of a QUEUE, where the departures
happen in arrival order, and that extra structure lets a deque do it in
O(1) amortised with no heap at all. Three versions of "report the maximum",
three different right answers, chosen by what else is guaranteed.
## Cost
Time O(q log q). Space O(q).
Constraints - `1 ≤ q ≤ 200000` - `-1000000000 ≤ v ≤ 1000000000` - Every `remove v` names a value that is currently in the bag.
Input
The first line contains one integer q.
Each of the next q lines is add v, remove v, or the single word max.
Output
Print one line for every max operation, in order: the largest value in the bag, or the word empty if the bag holds nothing. If the log contains no max 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.