Measuring the Median
Time limit1sMemory limit256 MB
Compute the sum of the medians of every contiguous window of length K over N temperature readings.
- Level
Medium6 of 10
- Topics
- Heap, Sliding window
- Solved
- No attempts yet
Problem
Meteorology mainly uses the median as its representative value. The hint defines the median.
A machine reads the temperature once per second, and it needs software. The machine has one small digital display, and every second that display shows the median of the temperatures measured during the last seconds.
The software is tested on a computer first, before it is loaded onto the machine.
Given the temperatures measured over seconds, write a program that computes the sum of the medians shown on the display. In other words, given numbers, compute the sum of the medians of the contiguous subsequences of length .
Input
The first line contains and . (, , )
Each of the next lines contains one measured temperature, given in order. A temperature is an integer between and , inclusive.
Output
Print the sum of the medians of all contiguous subsequences of length .
Hint
The median of numbers is the -th smallest of them. Counting starts at 1, and the added 1 makes the index land exactly in the middle when is odd.
For example, the median of is , and the median of is .