Editorial for COCI '15 Contest 2 #6 Država


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.

There are two conceptually different solutions that are both based on the following observation:

If there is a county with at least K cities in it, the prime minister will be happy. The proof of this relatively simple claim is obtained by using Dirichlet's principle and is left as an exercise to the reader.

First solution

Let us notice that we can use binary search by distance D if for a fixed D we know how to check if there is a layout of roads such that the prime minister is happy.

For each city, let us define a rectangle (so-called "bounding box") that contains all points with the smaller x coordinate, which x and y coordinates differ for at most D from the city we are currently observing.

It is sufficient to notice that, if there are more 8K cities in that rectangle, then there exists a county with at least K cities. The reason for this is that a rectangle can be split into 8 smaller squares of side length D/2. All cities inside the smaller square are in the same county and, given the aforementioned lemma, we know that we can make the prime minister happy.

Additionally, all cities with distance to the current city smaller than D are located inside the rectangle.

Let us sort the cities by their x coordinate and process them in this order using the sweep line technique.

When processing a city, we insert it into a binary tree where the cities are sorted by their y coordinate. We remove from the tree all cities which x coordinate differs from the x coordinate of the current city for more than D.

Now we can efficiently iterate over all cities inside the rectangle and connect with roads the cities that are distant for at most D from the current city. If at some point we notice that a rectangle contains more than 8K points, we can conclude that we can make the prime minister happy and we don't need to check any further.

After that, we perform a DFS algorithm in order to form counties and, in the end, use dynamic programming to check if there is a subset of cities in a county that has the sum divisible by K.

The time complexity of this algorithm is \mathcal O(NK \log D_\max) where D_\max is the maximal possible value of number D.

Second solution

This solution takes advantage of the fact that, for each city, it is enough to know of its K closest cities. (That fact is obtained directly from the first lemma.)

The solution consists of two parts:

  1. Finding the K nearest neighbours for each point.
  2. Forming counties and checking if the newly made county contains the required subset of cities.

We process the cities from left to right and use P to denote the minimal P such that we have found a city with at least K neighbours so far that are distant for at most P. For the current city we calculated the same rectangle from the first solution, go over all points in the rectangle and find the K^\text{th} nearest point from the rectangle and change the value P. Notice that the rectangle consists of at most 8K points using a similar argument to the one from the first solution.

This part of the solution is of complexity \mathcal O(NK + N \log N).

When we have found NK potential roads for each city, we sort these roads by length and connect them respectively. The counties are maintained using the union-find data structure. When connecting two cities from different counties, we go over all the cities from the smaller county, insert them into the larger county and calculate the remainders \operatorname{mod} K from the subsets of the newly formed county. With careful implementation, this part of the solution is of complexity \mathcal O(NK \log N) if we use the algorithm that joins the smaller component to the larger one in union-find.


Comments

There are no comments at the moment.