Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
The first thing we need to do is find the orientation of the robot. It can be done by issuing the command move L
, where
is the total length of the track, calculated easily from input data. The command will cause the robot to complete one full circle along the track, counting each turn exactly once. Since the track is a polygon that doesn't intersect itself, it is easy to show that the number of left turns will be greater than the number of right turns if and only if the robot is moving counterclockwise.
The next step is moving the robot to some vertex (turn). It can be solved using binary search. Observe that, if the robot is currently at some position
with distance exactly
to the closest vertex, then
is the smallest number such that move Y
returns
for all
and a value different from
for all
. It follows that we can find
by binary search, by following each move Y
command (whose result we search over) by a move L-Y
command in order to return to position
before the next iteration. After finding
, we simply execute move X
, which is guaranteed to place the robot in some polygon vertex - we just don't know which one.
The last step is finding the exact vertex where we have moved the robot. In the beginning, all vertices are possible candidates for the robot's position. Now we need to find, for some two candidates
and
, a number
such that the command move Y
returns a different value depending on whether the robot is in
or in
. Then, after executing move Y
, we can eliminate at least one of the vertices as a possible candidate (perhaps even both). Then we can return to our starting vertex using the command move L-Y
and repeat the process until only one candidate vertex is left, giving us the exact position of the robot. Now we simply need to find a good value of
for given vertices
and
: it can be done by simulation, walking along the track and constructing the sequence [segment_length, turn_orientation, segment_length, …] for one complete circuit around the track, with each of the two vertices as a starting position. The sequences for
and
must differ because the problem statement guarantees that a unique solution exists. The first difference between the two sequences gives us the needed value of
.
Since the first step uses
command, the second at most
commands, and the third
commands, we are guaranteed to use less than
commands in total.
Comments