Editorial for COCI '19 Contest 4 #2 Spiderman
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 .
For an additional 14 points, you should have used the fact that there are mere 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 where represents the number of different heights among the skyscrapers.
In test cases worth an additional 14 points, you could have assumed that . In other words, from a skyscraper of height it was possible to jump on a skyscraper of height if is a divisor of . Finding all divisors of a certain number can relatively easily be done in . Therefore, we have an algorithm of time complexity 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 ), 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 meters high?". The answer is, naturally, from skyscrapers of height or or or …. Let denote the height of the highest possible skyscraper, then we can jump on a skyscraper of height from 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 . For the first skyscraper we have candidates to traverse, for the second one we have candidates to traverse, …, and for the last one we have candidates to traverse. Therefore, the total number of candidates to traverse is 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