Editorial for COI '11 #2 Mjesec


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 L 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 P with distance exactly X to the closest vertex, then X is the smallest number such that move Y returns (0, 0) for all Y < X and a value different from (0, 0) for all Y \ge X. It follows that we can find X 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 P before the next iteration. After finding X, 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 A and B, a number Y such that the command move Y returns a different value depending on whether the robot is in A or in B. 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 Y for given vertices A and B: 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 A and B 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 Y.

Since the first step uses 1 command, the second at most 2 \log(\text{max segment length}) commands, and the third 2N commands, we are guaranteed to use less than 5\,000 commands in total.


Comments

There are no comments at the moment.