Editorial for COCI '19 Contest 4 #2 Spiderman


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's solve the easier versions of the task first that were given in the Scoring section.

The first partial score worth a total of 14 points could have been solved by a simple simulation. In other words, we could have used two nested loops to fix each pair of skyscrapers and check whether Peter can jump from one to the other. The time complexity of this solution is \mathcal O(N^2).

For an additional 14 points, you should have used the fact that there are mere 2000 different heights among all skyscrapers. We can store for each of these heights how many skyscrapers have it and now the task boils down to a solution very similar to the one described above. Instead of visiting each pair of skyscrapers, we will visit each pair of heights and store the solution for each height. The time complexity of this solution is \mathcal O(M^2) where M represents the number of different heights among the skyscrapers.

In test cases worth an additional 14 points, you could have assumed that K = 0. In other words, from a skyscraper of height h_i it was possible to jump on a skyscraper of height h_j if h_j is a divisor of h_i. Finding all divisors of a certain number x can relatively easily be done in \mathcal O(\sqrt x). Therefore, we have an algorithm of time complexity \mathcal O(N \sqrt{max_H}) which should score these 14 points. If you are not familiar with a popular algorithm that finds all the divisors of a given number, feel free to visit this link. (DMOJ curator's note: Your browser does not support java. :blobsad:)

Solving the entire problem could have been done via a slight modification of the previous algorithm (hint: observe all divisors of h_i-k), but here we will explain a slightly different and faster algorithm. Let's ask ourselves: "From which skyscrapers can we jump on a skyscraper that is h_i meters high?". The answer is, naturally, from skyscrapers of height K or K+h_i or K+2h_i or …. Let max_H denote the height of the highest possible skyscraper, then we can jump on a skyscraper of height h_i from \sim \frac{max_H}{h_i} different heights. The question presents itself, can we traverse over all candidate heights for each skyscraper within the given time limit? Let's assume the worst case in which all heights of skyscrapers are different (otherwise we just use the same trick from the second subtask). Therefore, the skyscrapers have heights 1, 2, \dots, max_H. For the first skyscraper we have \sim \frac{max_H}{1} candidates to traverse, for the second one we have \sim \frac{max_H}{2} candidates to traverse, …, and for the last one we have \sim \frac{max_H}{max_H} candidates to traverse. Therefore, the total number of candidates to traverse is \sim max_H \ln max_H which is small enough to pass all test cases. If you are struggling with the last observation, we suggest you familiarize yourself with the complexity analysis of a famous algorithm called Sieve of Eratosthenes or simply check out this document.


Comments

There are no comments at the moment.