Editorial for Wesley's Anger Contest 3 Problem 7 - Mirrors


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.

Author: wleung_bvg

Subtask 1

For N = 2, there is always a solution. We can select any two arbitrary points. For N = 3 or N = 4, we need to determine if there is a path of at least 3 points. We can try all possible triplets of 3 nodes and determine if the direction vector between the first and second points is equal to the direction vector between the second and third points after reflecting on the second mirror.

Time Complexity: \mathcal{O}(N^3)

Subtask 2

For the second subtask, we can select a pair of points to use as an initial direction vector, and repeatedly find the next mirror on the path taken by the laser and reflect on that mirror. This can be done by simply iterating through each mirror, and determining if the vector from the last mirror hit to the mirror in consideration is in the same direction as the direction vector, which takes \mathcal{O}(N) time for each reflection. Although it seems that this takes \mathcal{O}(N^4) time in total, it is almost impossible for each possible pair of initial direction vectors to create a path of length N due to the random location of mirrors. Care should be taken to avoid 64-bit integer overflow; if the direction vector cannot be reduced to a value where the absolute value of both coordinates is less than 10^6, then it is impossible for there to be another mirror that it can hit.

Time Complexity: \mathcal{O}(N^3)

Subtask 3

For the third subtask, we can observe that if there is a path that passes through \lceil \frac{3N}{4} \rceil of the mirrors, then if a random mirror is selected, it's very likely that it is on this path. Thus, we do not need to try all pairs of points for the initial direction vector, and instead select a few random points, and try all N - 1 direction vectors for each of those random points. Since each trial has a 75\% chance of success, if we select 5 random points, there is a 99.9\% chance our solution is correct if such a path exists. If we do not find a path, then we can assume that a path does not exist.

Time Complexity: \mathcal{O}(N^2)

Subtask 4

For the final subtask, we need to quickly find the next mirror on the path given the last mirror the laser hit, and a direction vector (a ray). There are multiple methods to accomplish this.

The first method involves dividing the coordinate plane (bounded by x, y \le 10^6) into a grid of \sqrt{N} \times \sqrt{N} squares of the same size. Since the mirrors are randomly scattered, the expected number of points in each square is 1. To determine if a point is on the ray, we can start with the current square, and move to an adjacent square that the ray also passes through. It can be seen that the ray passes through at most 2 \times \sqrt{N} - 1 squares.

Time Complexity: \mathcal{O}(N \times \sqrt{N})

Alternatively, one can observe that on average, the number of integer coordinates (with x, y \le 10^6) on the ray is fairly small. We can iterate through all of them by taking constant multiples of the reduced direction vector.

Edit: Turns out that there are cases that can break this solution that take around 1.5 times the time limit, however they were not included in the data.


Comments

There are no comments at the moment.