Under construction

Still being built. Things may change

Median filter

Time limit1sMemory limit128 MB

Summary
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 u[k]u[k] to every integer kk. 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 (−2,3)(-2, 3), (0,1)(0, 1), (3,7)(3, 7), (5,7)(5, 7). Only u[−2]=3u[-2] = 3, u[0]=1u[0] = 1, u[3]=7u[3] = 7, u[5]=7u[5] = 7 are given explicitly, and every other value lies on the line through two neighbouring points.

  • The values on (−∞,0](-\infty, 0] come from the line through (−2,3)(-2, 3) and (0,1)(0, 1).
  • The values on [0,3][0, 3] come from the line through (0,1)(0, 1) and (3,7)(3, 7).
  • The values on [3,∞)[3, \infty) come from the line through (3,7)(3, 7) and (5,7)(5, 7).

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 2d+12d + 1 reads the signal u[k]u[k] and outputs the signal y[k]=median⁡{u[k−i]:−d≤i≤d}y[k] = \operatorname{median}\{u[k - i] : -d \le i \le d\}. In other words, sort u[k−d],u[k−d+1],…,u[k+d]u[k - d], u[k - d + 1], \ldots, u[k + d] 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 2d+12d + 1 when the input signal is given by points.

Input

The first line contains an integer NN (2≤N≤502 \le N \le 50), the number of points that describe the input signal.

Each of the next NN lines contains two integers, the coordinates of one point. Both coordinates are at most 10910^9 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 dd (0≤d≤500 \le d \le 50), where 2d+12d + 1 is the width of the filter.

Output

Describe the output signal yy by its corners. An integer kk is a corner of yy if y[k+1]−y[k]≠y[k]−y[k−1]y[k + 1] - y[k] \ne y[k] - y[k - 1]. The signal yy has finitely many corners.

If yy has at least one corner, let the corners be k1<k2<⋯<kmk_1 < k_2 < \cdots < k_m. Print M=m+2M = m + 2 on the first line, then print the following MM points in this order, two integers per line.

(k1−1, y[k1−1]), (k1, y[k1]), (k2, y[k2]), …, (km, y[km]), (km+1, y[km+1])(k_1 - 1,\ y[k_1 - 1]),\ (k_1,\ y[k_1]),\ (k_2,\ y[k_2]),\ \ldots,\ (k_m,\ y[k_m]),\ (k_m + 1,\ y[k_m + 1])

The slope of yy is constant on (−∞,k1](-\infty, k_1] and on [km,∞)[k_m, \infty), so these points interpolate yy exactly.

If yy has no corner, then it is a single line. Print 22 on the first line, then print the two points (x1,y[x1])(x_1, y[x_1]) and (x1+1,y[x1+1])(x_1 + 1, y[x_1 + 1]), where x1x_1 is the first coordinate of the first input point.

Every printed number fits in a signed 64-bit integer type.

Examples3

  1. Example 1

    Input
    4
    -2 3
    0 1
    3 7
    5 7
    2
    
    Expected output
    5
    -3 4
    -2 3
    1 3
    3 7
    4 7
    
  2. Example 2

    Input
    11
    -8 -2
    -7 -2
    -6 7
    -5 -2
    -3 -2
    -2 5
    -1 0
    2 3
    3 -3
    4 5
    5 6
    2
    
    Expected output
    8
    -4 -2
    -3 -2
    -2 0
    0 2
    1 1
    3 3
    4 5
    5 6
    
  3. Example 3

    Input
    3
    -1 10000
    0 0
    1 -10000
    50
    
    Expected output
    2
    -1 10000
    0 0