A binary heap will let you insert and remove a minimum in O(log n) , and the rest in O(1) .
The only tricky part is deleting the oldest element after filling the array. To do this, save another array:
time[i] = at what position in the heap array is the element added at time i + 100 * k.
Every 100 iterations you increase k .
Then, when the array is filled for the first time, you delete heap[ time[0] ] , when it is filled the second time, when you delete heap[ time[1] ] , ... when it is filled for the 100th time , you wrap around and delete heap[ time[0] ] again, etc. When it is filled for k th times, you will remove heap[ time[k % 100] ] (100 is your array size).
Be sure to update the time array when you insert and delete elements.
Removing an arbitrary element can be done in O(log n) if you know its position: just replace it with the last element in the heap array and sift the element that you changed.
source share