Differential Pulse Code Modulation
InterviewTime limit8sMemory limit512 MB
Given a codebook of step values and a signal, choose a code per sample so the reconstructed signal stays within 0 to 255 and minimizes the sum of squared errors against the input.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Implementation, Math, Array
- Solved
- No attempts yet
Problem
Differential pulse code modulation is one of the compression methods used mainly for compressing speech signals.
A speech signal is handled as a sequence of integers (an impulse sequence) on a computer. The integer sequence is obtained by sampling the input signal at fixed time intervals and recording the amplitudes. In general, this integer sequence tends to have neighboring values close to each other. Differential pulse code modulation exploits this tendency by encoding the difference between neighboring values to improve the compression rate.
In this problem, we consider choosing the difference value from a fixed set of values. We call this set of values a codebook. The decoded speech signal yn is defined by the following formula.
y**n = y**n - 1 + C[k**n]
Here, k**n is the output sequence produced by the program, and C[j] is the j-th value of the codebook. However, if the result of the addition for y**n is less than 0, it is rounded to 0, and if it is greater than 255, it is rounded to 255. Also, the value of y0 is 128.
Given the input signal and the codebook, write a program that chooses the output sequence so that the sum of squared differences between the original input signal and the decoded output signal is minimized, and outputs that minimum sum of squared differences.
For example, when compressing the sequence 131, 137 using the codebook value set {4, 2, 1, 0, -1, -2, -4}, compressing it to the sequence y**0 = 128, y**1 = 128 + 4 = 132, y**2 = 132 + 4 = 136* makes the sum of squares (131 - 132)^2 + (137 - 136)^2 = 2, which is minimal.
Also, when compressing the sequence 131, 123 using the same codebook value set {4, 2, 1, 0, -1, -2, -4}, choosing y**0 = 128, y**1 = 128 + 1 = 129, y**2 = 129 - 4 = 125* gives a smaller sum of squares (131 - 129)^2 + (123 - 125)^2 = 8; unlike the previous example, not adopting +2, which is closer to 131, yields the smaller sum.
The two examples above are the first two cases of the sample input.
Input
The input consists of multiple data sets. The format of each data set is as follows.
N M
C1
C2
...
CM
x1
x2
...
xN
The first line specifies the size of the input data set. N is the length (number of samples) of the input signal to be compressed. M is the number of values contained in the codebook. N and M satisfy 1 ≤ N ≤ 20000 and 1 ≤ M ≤ 16.
The following M lines describe the codebook. Ci represents the i-th value contained in the codebook. Ci satisfies -255 ≤ Ci ≤ 255.
The following N lines describe the input signal. xi is the i-th value of the integer sequence representing the input signal. xi satisfies 0 ≤ xi ≤ 255.
All input items within a data set are integers. The end of the input is indicated by a line consisting of exactly two zeros separated by a single space.
Output
For each data set, output the minimum sum of squared differences between the original input signal and the decoded output signal on one line.