Editorial for COCI '15 Contest 4 #6 Endor
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us observe two consecutive chameleons moving to the right and are located at coordinates and , and a chameleon at the coordinate moving to the left. Between the collision with two chameleons moving to the right, he will pass meters. We can see that the distance traveled doesn't depend on the coordinate of the chameleon moving to the left.
The task is further solved using dynamic programming. Let be an array of length that denotes the distance traveled in each color a chameleon colored in moving to the left will take in consecutive collisions if it had just collided with a chameleon moving to the right. Using the aforementioned statement, we can easily calculate the value of function .
Finally, for each chameleon moving to the left, we calculate:
- distance traveled until the first collision;
- distance traveled between consecutive collisions (calculated using );
- distance traveled from the last collision to the end
The time complexity of this algorithm is .
Comments