Official

A - Moving Slimes Editorial by evima


Consider how to solve the case \(K=N\) without simulation.

The answer does not change even if we assume that the slimes do not merge. We may do this by assuming that slimes at the same coordinate do not affect the velocity.

Let \(X_i\) be the coordinate of the center of mass of the first \(i\) slimes, \(Y_i\) be the coordinate of the center of mass of the last \(N-i\) slimes, and \(Z_i=Y_i-X_i\).

Consider the rate at which \(Z_i\) decreases. As long as \(A_i<A_{i+1}\), it decreases at a rate of \(N\). When \(A_i=A_{i+1}\), the rate of decrease is less than \(N\). The degree of decrease varies depending on the number of slimes at the same coordinate as \(A_i\), but in any case, it is less than \(N\).

When all slimes are at the same coordinate, \(Z_i=0\) holds for all \(1 \leq i \leq N-1\). Therefore, \(\max(Z_i)/N\) is obtained as a lower bound of the answer.

Next, consider the time when \(A_i=A_{i+1}\). Suppose \(A_i<A_{i+1}\) until time \(t=Z_i/N\). Then, since the rate of decrease of \(Z_i\) has been \(N\) all the time, \(Z_i\) becomes \(0\) at time \(t\). At this time, it is clear that \(A_i=A_{i+1}\) also holds. In the end, we can see that the first time \(A_i=A_{i+1}\) holds is at most \(Z_i/N\).

When all slimes are at the same coordinate, \(A_i=A_{i+1}\) holds for all \(1 \leq i \leq N-1\). Therefore, \(\max(Z_i)/N\) is obtained as an upper bound of the answer.

Since the upper and lower bounds match here, we know that the answer is exactly \(\max(Z_i)/N\).

Based on this consideration, we solve the case \(K<N\). For each \(1 \leq i \leq K-1\), consider maximizing the difference between the coordinates of the center of mass of the first \(i\) slimes and the last \(K-i\) slimes. This can be achieved by simply taking the first \(i\) and the last \(K-i\) slimes from the given \(N\) slimes.

By implementing the above, the problem is solved in \(O(N)\).

Sample Implementation

posted:
last update: