Problem 399
Median of a Data Stream
Hard · 30 pts · Heap, Design, Data Stream, Sorting
Problem
Numbers arrive one at a time. After each new number, print the median of all numbers received so far. With an odd count, the median is the middle value in sorted order; with an even count, it is the average of the two middle values. Print every median with exactly one digit after the decimal point (it is always a whole number or ends in .5). Keeping the smaller half in a max-heap and the larger half in a min-heap gives O(log N) per number.
Input
First line: Q. Second line: the Q numbers in the order they arrive.
Output
Print Q medians, separated by spaces.
Constraints
1 <= Q <= 100000; -100000 <= value <= 100000
Example input
4 5 15 1 3
Example output
5.0 10.0 5.0 4.0
Explanation
After 5 the median is 5.0. After 5 15 it is (5 + 15) / 2 = 10.0. After 5 15 1 the sorted numbers are 1 5 15, so the median is 5.0. After adding 3 they are 1 3 5 15, giving (3 + 5) / 2 = 4.0.