AmazonOA #328
Process Queue
Estimated MediumJudge-ready
Topics (AI-labelled)SimulationIn-place Arrays
All processes start in the queue, and wait[i] is the deadline, counted from time 0, up to which process i can wait. Time t starts at 0. Each second, first process and remove the one process at the head of the queue, then remove every other remaining process whose deadline is ≤t, including expired items in the middle or at the end of the queue. Record the number remaining at the end of that second, until the queue is empty; the initial n before any processing is not recorded.
Input
The first line contains n and the second line contains wait. On this site 1≤n≤200000, 0≤wait[i]≤10^9, and a deadline of 0 is valid. The last output item must be 0.
Output
Output the queue length at the end of each second, separated by spaces, ending with 0.
Example 1
Input:
4
2 2 3 1
Output:
3 1 0
Example 2
Input:
5
4 3 1 2 1
Output:
4 1 0
Example 3
Input:
3
0 0 9
Output:
1 0
Example notes
The answers are 3 1 0; 4 1 0; and 1 0. In the third example, at time 0 the first 0 at the head is removed first, then the other expired 0 is removed, and 9 is left to be processed at time 1. The source only clears expired items at the head, so it misses those in the middle and at the end.
Editorial
Loading solution…