Median filter
Time limit1sMemory limit128 MB
Given a piecewise-linear integer signal by its corner points, output the corners of its median-filtered signal of width 2d+1.
- Level
Hard8 of 10
- Topics
- Math, Implementation, Sorting, Geometry
- Solved
- No attempts yet
Problem
A digital signal is a function that assigns an integer to every integer . To describe an endless signal with finitely many numbers, you give some values explicitly and compute the rest by linear interpolation.

The signal in the picture above can be described by the points , , , . Only , , , are given explicitly, and every other value lies on the line through two neighbouring points.
- The values on come from the line through and .
- The values on come from the line through and .
- The values on come from the line through and .
The same signal can be described by arbitrarily many points, as long as the lines through neighbouring points interpolate the signal exactly.
Median filters are common for removing noise from a signal. A median filter of width reads the signal and outputs the signal . In other words, sort and take the value in the middle of the sorted sequence.
Below, the pictures on the left show input signals and the pictures on the right show the output of a filter of width 5.




Write a program that computes the output of the median filter of width when the input signal is given by points.
Input
The first line contains an integer (), the number of points that describe the input signal.
Each of the next lines contains two integers, the coordinates of one point. Both coordinates are at most in absolute value. The points are given by increasing first coordinate, and two different points have different first coordinates. Every interpolated value is an integer.
The last line contains an integer (), where is the width of the filter.
Output
Describe the output signal by its corners. An integer is a corner of if . The signal has finitely many corners.
If has at least one corner, let the corners be . Print on the first line, then print the following points in this order, two integers per line.
The slope of is constant on and on , so these points interpolate exactly.
If has no corner, then it is a single line. Print on the first line, then print the two points and , where is the first coordinate of the first input point.
Every printed number fits in a signed 64-bit integer type.