Editorial for Google Code Jam '18 Qualification Round Problem A - Saving The Universe Again
Submitting an official solution before solving the problem yourself is a bannable offence.
Test set 1
Since there is at most one C
instruction in this test set, we
can solve the two cases independently.
If there is no C
instruction in , then none of our swaps
will have any effect, so all we can do is check whether the damage of the
beam exceeds .
If there is one C
instruction in , then we can try every
possible position for the C
instruction in the program. Assuming
that there is at least one position for the C
instruction that
causes the total damage not to exceed , we can choose the scenario
that requires the fewest swaps; the number of required swaps for a scenario
is equal to the distance between the original and final positions of the
C
instruction.
Test set 2
To solve test set 2, we will first derive a formula to compute the total
damage based on the positions of the C
and S
instructions in . Let and be the number of
C
and S
instructions in , respectively.
Let be the number of S
instructions to the right of
the -th C
instruction, where uses -based indexing.
Note that the -th C
instruction will increase the damage of the
subsequent beams by . For example, in the input program
CSSCSSCSS
, initially, all of the S
instructions will
inflict a damage of . Consider the damage dealt by the last S
instruction. Since the robot has been charged twice, the damage output by the
last instruction will be . Alternatively, we see that the damage,
(initial damage) (damage caused by the first C
)
(damage caused by the second C
). By breaking
down the damage by each S
instruction in the same manner, the
total damage output, , of the input program is given by:
Next, we investigate how each swap affects the amount of damage. A swap on
adjacent characters which are the same will not affect the equation. When we
swap the -th C
instruction with a S
instruction to
its right, the value of will decrease by since now there is
one less S
than before. On the other hand, swapping the -th C
instruction with an S
instruction on its left will increase the
value of by . Note that in either case, we will only modify
the value of , and the other values will remain the same. This
suggests that we should only ever swap adjacent instructions of the form
CS
.
Therefore, executing swaps is equivalent to reducing the values of s such that the total amount of reduction across all s is . We want the total damage (according to the above equation) to be minimized. Clearly, we should reduce the values of that contribute to the largest damage output, while making sure that each of the s is nonnegative.
Intuitively, all of this math boils down to a very simple algorithm! As long
as there is an instance of CS
in the current program, we always
swap the latest (rightmost) instance. After each swap, we can recompute the
damage and check whether it is still more than . If it is not, then
we can terminate the program. If we ever run out of instances of
CS
to swap, but the damage that the program will cause is still
more than , then the universe is doomed.
Test Data We recommend that you practice debugging solutions without looking at the test data.
Comments