Editorial for COCI '12 Contest 5 #5 Rotiraj
Submitting an official solution before solving the problem yourself is a bannable offence.
We will arrive at a solution to this problem in several steps. Let us first simplify the problem.
Notice that each operation has an inverse operation, which is of the same type, but with the opposite sign, that is, rotating in the opposite direction. This means that we can obtain the starting sequence by applying inverses of the input operations, in reverse order, to the final sequence.
Also, notice that any rotation to the left can be replaced with an equivalent rotation to the right. Rotation of the whole sequence by to the left can be replaced by a rotation of the sequence by to the right; analogously for section rotations.
The next simplification is replacing the numbers in the sequence with their positions, and then applying operations to the position sequence. It is easy to restore the original numbers from the positions after finishing with the rotations.
The problem has been reduced to starting with the sequence and applying 2 types of rotation-to-the-right operations to it. A simple simulation of each operation leads to the complexity , which is sufficient for of total points.
Looking at numbers and (modulo ), observe that their distance (modulo ) remains equal to after any rotation operation. Therefore, it is sufficient to track only the positions of numbers while applying rotations. In the end, the positions of the remaining numbers can be reconstructed as follows: .
This improved simulation of each operation has the complexity , which is sufficient for of total points.
For further optimization, we can track the positions of the numbers (modulo ). After each operation, their positions are some rotation of the general form: . This rotation can be tracked by a single global variable representing the total number of rotations to the right needed to transform the starting sequence of positions to the current one. Both types of rotations to the right simply add to this total rotation. Thus, the position of each of the first numbers in its current section is easily obtainable from the total rotation.
To unambiguously determine the position of the first numbers, and by extension all numbers, we also need to track the current section for each of the numbers.
An operation of type 1 doesn't change the contents of the sections. An operation of type 2 can be processed modulo , with the whole part tracked by another global variable since it applies to all numbers equally. Looking at the first numbers, an operation of type 2 moves a total of numbers (where is the remainder modulo ) to the following section. We also know exactly which numbers are moved. From the general form of rotation, we can see that it applies to the last numbers of the sequence: . Since these numbers are always contiguous, we can increase the interval in time . In the array tracking the sections for each of the numbers, we add a to the beginning and a to the end of the interval.
The positions of the first numbers are thus obtained from two parts. One defines the section that each number is in, and the other defines the position inside the section. After obtaining the positions of the first numbers, the positions of the remaining numbers are derived as described above.
Since each operation is now processed in time , the total complexity is .
Comments