#P8963. Minimize Absolute Difference via Array Construction
Minimize Absolute Difference via Array Construction
Minimize Absolute Difference via Array Construction
You are given an integer array \(x\) of length \(n\) and an integer \(V\). You need to construct an integer array \(y\) of the same length \(n\) and choose an integer constant \(k\) such that the following conditions hold:
- For all indices \(i, j\) with \(1 \le i, j\) and \(i+j \le n\), the following equation is satisfied: \[ y_{i+j} = y_i + y_j + k \] (The formula is in \(\LaTeX\) format.)
- The distance \[ d(x,y) = \sum_{i=1}^{n} \left| x_i - y_i \right| \] is minimized.
- Each element in \(y\) must satisfy \(-V \le y_i \le V\) for \(1 \le i \le n\).
Observation: It can be shown by induction that the recurrence uniquely determines \(y\) in terms of \(y_1\) and \(k\). In fact, for \(1 \le m \le n\), we have:
[ y_m = m \cdot y_1 + (m-1)\cdot k. ]
Your task is to choose integers \(y_1\) and \(k\) so that all \(y_m\) are within \([-V,V]\) and the total absolute difference \(d(x,y)\) is minimized. Then output the array \(y\) (space separated).
inputFormat
The first line contains two integers \(n\) and \(V\) (the length of the array and the bound for \(y\)).
The second line contains \(n\) integers \(x_1, x_2, \dots, x_n\) separated by spaces.
outputFormat
Output \(n\) integers representing the array \(y\) (space separated) that minimizes \(d(x,y)\) while satisfying the conditions.
sample
3 10
1 2 3
1 2 3