RMID2 - Running Median Again
The first line of the problem says "You will be given some integers in non-decreasing order". The problem asks you to report and remove the median of the list every time it is queried.
Having solved this problem, Danish now begins to wonder how to solve the problem if the input is in any order (not necessarily non-decreasing order as mentioned above).
Can you help him?
Your task is to take as input a list of positive integers. Whenever -1 is given as input, you must output the median of the list, and remove it from the list. Take the smaller element as the median in case of even number of elements.
The input contains several test caes.
The first line contains an integer t, the number of test cases.
Each test case has several lines, each containing an integer n (<=10^9) . If n is positive, add it to the list. n=-1 indicates a median query (there will be no median query if the list is empty). The test case is terminated by n=0.
In each test case, there will be upto 10^5 integers to be added to the list, and upto 10^5 median queries.
For each median query as described above, print the median on a new line.
Input: 1 9 10 2 5 1 18 -1 -1 4 3 -1 8 7 -1 0 Output: 5 9 3 7
using policy based data str in c++ makes easy AC
used BIT here , but how to do it using heaps??
Nice Problem use both max and min heap
nlog(n) giving TLE @@, so difficultLast edit: 2020-05-10 11:59:20
TLE in java.
Variational question to traditional "Median In A Stream". Nice Question
Very Strict Time Limit. Got AC after many TLE. Use scan/printf if fast_io in c++ not works.
Finally AC after 3 WA and 1 TLE :( , Good question learned a new concept :)
Getting TLE when not using FAST I/O )-