Live Latency Median
A monitoring agent watches a web service and records the response time of every request, in milliseconds. The dashboard shows one number next to the service: the median response time of all requests seen so far, refreshed after every new sample.
The agent receives n samples t_1, t_2, …, t_n in order. After the i-th sample arrives, sort
the first i samples in non-decreasing order and take the element at position ⌈i/2⌉ (1-based).
That is, for an odd count take the middle element, and for an even count take the lower of the two
middle elements. Report this value after every sample.
Input
The first line contains n (1 ≤ n ≤ 2·10^5), the number of samples.
The second line contains n integers t_1, t_2, …, t_n (1 ≤ t_i ≤ 10^9), the response times in
milliseconds, in the order they arrive. Values may repeat.
Output
Print n lines. The i-th line must contain the median of t_1, …, t_i as defined above.
Notes
In the first example, after the fourth sample the times seen are 40, 120, 35, 90. Sorted, they are
35, 40, 90, 120; the two middle elements are 40 and 90, and the dashboard shows the lower one,
40. After the fifth sample (60) the sorted list is 35, 40, 60, 90, 120 and the median is 60.
The reported value is always one of the samples; it is never an average.