2

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.

Gary Ye
  • 847
  • 1
  • 7
  • 9
  • 2
    See other question: ["Finding an increasing sequence a[] which minimizes sigma(abs(a[i]+c[i]))"](http://stackoverflow.com/q/10460861). Despite its name it discusses nondecreasing sequence instead of strictly increasing one. But difference is minimal because you can subtract "iota" sequence {0,1,2,...}, find nondecreasing sequence, then add "iota" sequence back. – Evgeny Kluev Jan 26 '15 at 12:03
  • Do you have similar problems like this one? – Gary Ye Jan 31 '15 at 20:55

0 Answers0