Editorial for COCI '13 Contest 6 #6 Graškrižja


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.

Let us sort the given traffic lights by their x-coordinate. Let x' be the middle (median) x-coordinate in that array. Let A be a set of given traffic lights to the left of x' and B to the right.

We will construct a harmless path between each pair of traffic lights a, b such that a is from the set A and b is from the set B. How? By adding new traffic lights to the locations (x', y) for each y-coordinate y from the sets A and B. Now for traffic lights a and b we have a harmless way (x_a, y_a) \to (x', y_a) \to (x', y_b) \to (x_b, y_b).

Now all we need to do is connect the traffic lights within the set A with one another, as well as those within the set B. We do this by recursively repeating the described procedure, specifically for set A and specifically for set B. This way of thinking is called divide and conquer.

A little bit of thinking is needed to be sure that the new traffic lights don't generate any new dangerous paths, but the implementation is reasonably simple.

What is the number of additional traffic lights? Given the fact that we divide the set into two parts, the maximal depth of recursion is \mathcal O(\log N). Let us observe an initial traffic light at the location (x, y). Worst case scenario, at every depth of the recursion, it will be included in a set and there it will generate a new traffic light (x', y). Therefore, one initial traffic light generates \mathcal O(\log N) new traffic lights, which gives us a total of \mathcal O(N \log N) new traffic lights. With careful implementation, the exact number turns out to be less than 700\,000.


Comments

There are no comments at the moment.