Two heaps and a clock
transcript
Some items are ready. Some are busy. Which one goes next? Two heaps answer it, and a clock links them.
The ready heap follows the rule of the problem. It might be most ads left. The busy heap follows time, not priority. Its top returns first. The clock links the two.
First, move due items to the ready heap. Then decide, using the ready top. If nothing is ready, jump the clock. Jump to the earliest return time, then go on.
The ready top tells you who goes next in line. The busy top tells you who returns next. Each pop costs about log n steps. So thousands of decisions stay fast.
Take one campaign with two hundred thousand ads. Stepping needs two billion ticks. A jump needs one step for each ad.
Move due items first, before every decision. Then decide from the ready top. Full breakdown in the lecture.