Editorial for TLE '16 Contest 3 P3 - Mysterious Package


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.

Authors: ZQFMGB12, d

In order to solve this problem, we want to perform a BFS on the graph where the classes are the nodes and the possible transfers are the edges. Thus, the distance to each node is the number of passes required to get the package to that node. Afterward, we can take each node that the girl is in, look for one with minimal distance, and if there is a tie, choose the smaller period.

In order to generate the graph, we should first sort all of the classes by period. Next, each student should have a data structure associated with them that represents which classes they have already attended. When we iterate through a class's students, we should form a one-way edge from each class in each student's data structure to the current class.

Time Complexity: \mathcal{O}(N^2 \cdot \max(s_i))

An alternative solution is to sort the classes according to p_i, then we can simulate the passing of the package to various students. We can use a map to store the (ID, passes) pairs. At period 0, there is only one pair, which is the CS Nerd's ID and his 0 passes.

Many classes can be taking place in the same period, so we need to store 2 maps. The first map represents students before the period, and the second map represents students after the period.

To process a class, the student with the least number of passes should be chosen. This student can choose to pass the package onto other classmates. However, the student should not pass the package to another person with the same number of passes. A class without an available student can be ignored.

The answer is updated only when there is a lower number of passes to the girl.

Time Complexity: \mathcal{O}(S \log S), where S = \sum_{i=1}^N s_i


Comments

There are no comments at the moment.