Editorial for BPC 1 S3 - Bus Stops
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask solution
Let equal .
If you focus on one color at a time, it is clearly optimal to place buses and stations in contiguous blocks with an offset. We can show that the number of visits for the color is equal to , where is the selected offset. This implies that an offset of works regardless of .
We can calculate the number of visits by simulating the buses' movement for the last steps (evaluating the described sum), and then adding to account for the full rotations.
Time Complexity:
Note that this subtask is designed to allow solutions to pass. This rewards people who make the observation about blocks, but simulate the buses' movement inefficiently.
Full solution
The only difference between the full solution and the subtask is how we calculate the number of visits. Instead of evaluating the sum iteratively, we break it into two arithmetic sums. The derivation is long, and left as an exercise to the reader.
Time Complexity:
Comments