#C11566. Median Power Levels

    ID: 40896 Type: Default 1000ms 256MiB

Median Power Levels

Median Power Levels

You are given several datasets detailing wizards and the power levels of the spells they cast. In each dataset, every wizard is represented by a line of space‐separated integers. The first integer in the line indicates the count of spells (this value can be ignored) and the remaining integers represent the power levels of the spells cast by that wizard.

Your task is to determine the median power level of all spells in each dataset. First, combine all the spell power levels from all wizards in the dataset. Then sort them in non-decreasing order. The median is defined as follows:

\( \text{median} = \begin{cases} a_{\frac{n}{2}}, & \text{if } n \text{ is even} \\ a_{\lfloor \frac{n}{2} \rfloor}, & \text{if } n \text{ is odd} \end{cases} \)

For example, if a dataset has the spell power levels [3, 10, 20, 30, 2, 15, 25, 4, 5, 10, 15, 20], after sorting, we get [2,3,4,5,10,10,15,15,20,20,25,30]. Since there are 12 elements (an even number), the median is the 6th element (i.e. 10).

inputFormat

Input Format:

  • The input consists of multiple lines.
  • Each wizard is defined on a single line with space-separated integers. The first integer represents the number of spells (which you can ignore) and the remaining integers are the spell powers.
  • A line containing a single integer (with no spaces) signals the start of a new dataset.
  • The input terminates with a line containing a single "0".

outputFormat

Output Format:

  • For each dataset, output the median power level as computed by the procedure described above.
  • Output the medians for all datasets on a single line, separated by a single space.
## sample
3
3 10 20 30
2 15 25
4 5 10 15 20
2
5 5 5 5 5
3 7 7 7
0
15 5