I've recently stumbled upon the following problem:
Given a sequence a[1], a[2], ..., a[n]
, find an array b[1] < b[2] < ... < b[n]
such that |a[1]-b[1]| + |a[2]-b[2]| + ... + |a[n]-b[n]|
is minimum.
I have already come up with some observations, but I still couldn't figure out any feasible solution.
I don't know the expected time complexity, nor do I know the solution. But it is given that n <= 10^6
.
The problem is from BOI 2003, and is called Sequence.
Input
9 4 8 20 14 15 18
Output
6 7 8 13 14 15 18
Since the difference is 13.