#C14331. Dynamic Real Estate Market Median
Dynamic Real Estate Market Median
Dynamic Real Estate Market Median
In this problem, you are asked to simulate a real estate market that dynamically updates as new house prices are added. Your task is to implement a module that supports two operations:
add [price]
: Add a new house with the given price.median
: Output the median house price in the current market.
The median is defined as follows: if the market has an odd number of houses, the median is the middle number in the sorted order. If there is an even number of houses, the median is \( \frac{a+b}{2} \), where \( a \) and \( b \) are the two middle prices. If no houses are present, the median should be considered as 0.0
.
You will receive input from standard input (stdin
) and must output your results to standard output (stdout
). Make sure that your solution handles dynamic updates correctly.
inputFormat
The first line of input is an integer \( Q \) representing the number of queries. The following \( Q \) lines each contain a query in one of the following formats:
add [price]
where [price] is an integer indicating the price of a house to be added.median
which requests the current median house price.
outputFormat
For each median
query, output the median price as a floating-point number on a new line. If the market is empty, output 0.0
.
5
median
add 300000
median
add 150000
median
0.0
300000.0
225000.0
</p>