Editorial for COCI '15 Contest 2 #6 Država
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  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  if for a fixed 
 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  coordinate, which 
 and 
 coordinates differ for at most 
 from the city we are currently observing.
It is sufficient to notice that, if there are more  cities in that rectangle, then there exists a county with at least 
 cities. The reason for this is that a rectangle can be split into 
 smaller squares of side length 
. 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  are located inside the rectangle.
Let us sort the cities by their  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  coordinate. We remove from the tree all cities which 
 coordinate differs from the 
 coordinate of the current city for more than 
.
Now we can efficiently iterate over all cities inside the rectangle and connect with roads the cities that are distant for at most  from the current city. If at some point we notice that a rectangle contains more than 
 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 .
The time complexity of this algorithm is  where 
 is the maximal possible value of number 
.
Second solution
This solution takes advantage of the fact that, for each city, it is enough to know of its  closest cities. (That fact is obtained directly from the first lemma.)
The solution consists of two parts:
- Finding the 
nearest neighbours for each point.
 - 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  to denote the minimal 
 such that we have found a city with at least 
 neighbours so far that are distant for at most 
. For the current city we calculated the same rectangle from the first solution, go over all points in the rectangle and find the 
 nearest point from the rectangle and change the value 
. Notice that the rectangle consists of at most 
 points using a similar argument to the one from the first solution.
This part of the solution is of complexity .
When we have found  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 
 from the subsets of the newly formed county. With careful implementation, this part of the solution is of complexity 
 if we use the algorithm that joins the smaller component to the larger one in union-find.
Comments