Official

E - Traveler Editorial by en_translator


The problem is, in which order the balls with the same color should be collected. Assume that there are \(k\) balls of color \(i\), with the coordinates \(x_1, x_2, x_3, \dots, x_k(x_1 < x_2 < x_3 < \dots < x_k)\).
Here, it is provable that we can assume that the last ball to be collected is located either at \(x_1\) or \(x_k\), regardless of the position after collecting the ball of the previous color. (It is meaningless to pass by a ball of color \(i\) without collecting it, and after visiting both \(x_1\) and \(x_k\), all the other balls are already passed by)
Therefore, the problem can be solved in a total of \(O(N)\) time with the following DP:
\(\mathrm{dp}[i][j] : \) the minimum time required to finish collecting all the balls of color \(i\), after which you are located at the same position of the leftmost (if \(j = 0\)) or the rightmost (if \(j = 1\)) ball of color \(i\). (We regard as if there are no such color that no ball is painted with)

Specifically, let \(l\) be the minimum, and \(r\) be the maximum, coordinates of the balls of color \(i\), then the minimum time required to reach the coordinates \(l\) after collecting all the balls of color \(i\) is, if the final coordinates after collecting all the balls of color \(i-1\) is \(x\),

  • if \(x \le r\): you move to \(r\) on your right, then move back to \(l\) on your left, so \((r - x) + (r - l)\)
  • if \(x \gt r\): you just have to come to \(l\) on your left, so \(r - x\)

We can similarly find the minimum time required to reach the coordinates \(r\) after collecting all the balls of color \(i\), starting from coordinates \(x\) after collecting color \(i - 1\), so one can transit from \(\mathrm{dp}[i -1]\) to \(\mathrm{dp}[i]\) in an \(O(1)\) time.

posted:
last update: