Editorial for IOI '16 P2 - Roller Coaster Railroad


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.

Subtask 1. To solve the first subtask, one could iterate over all n! possible permutations of the special sections. Once the permutation is fixed, the only thing left is to compute the required railway segment length between every two consecutive sections: if the i^\text{th} section is followed by the j^\text{th} one, then the required length is \max(0, t[i]-s[j]).

Subtask 2. One could use a standard dynamic programming approach to solve the second subtask. Every subset of the given sections can be encoded as a bitmask (from 0 to 2^n-1). Let ans[mask][i] denote the minimum total railway length, considering only the sections from the set encoded by mask with an additional restriction that the i^\text{th} special section should go first.

  1. If mask = 2^i (there is only one special section), the answer is clearly 0.
  2. Otherwise, there must be a section j following the i^\text{th} one, so:

\displaystyle ans[mask][i] = \min_{j \ne i, j \in mask} \left(ans[mask-2^i]+\max(0, t[i]-s[j])\right)

The answer to the problem is \min_i (ans[2^n-1][i]). One could also notice that this subtask is an instance of the well-known TSP (travelling salesman problem), where the special sections represent the cities, and the distance function is the required railway segment length.

Subtask 3. Let's add an additional special section with s = \infty and t = 1: now we can introduce a restriction that the train must have speed exactly 1 km/h at the end of the journey (here, \infty represents any number greater or equal than any of the numbers given in the input data — 10^9 would suffice).

Consider an infinite graph, with a vertex set 1, 2, 3, \dots and there is an edge from s[i] to t[i] for every section i (including the added one). For every positive integer x, consider the balance value: (number of edges going over the segment [x, x+1] from left to right) minus (number of edges going over the segment [x, x+1] from right to left). In the picture above, one can see three edges going from left to right (red) and two going in the opposite direction (green), so the balance is 1.

If one aims to start from 1 km/h and end with the same speed, then the train must cross the segment [x, x+1] equal number of times in both directions. If the balance is positive then it's necessary to add an additional green edge to slow down the train, so at least one railway segment is required and the answer is not zero. If the balance is negative it just means that the train needs to be accelerated at some point, so one can add as many additional red edges as needed for free.

Once the balance equals zero for every x, it is sufficient to check whether the resulting graph is connected or not. If the graph is not connected, then the answer is clearly not zero: to go from one component to the other it's needed to slow down the train at least once, so an additional railway segment is required. If the graph is connected, then, since all the balances are zero, for every vertex x its in-degree equals its out-degree, and thus there exists an Euler cycle in this graph, from which one could construct a valid sections arrangement.

To do this efficiently, one needs to consider only the "interesting" values of x, which are given in the input data, and instead of considering segments [x, x+1] one should consider [interesting_i, interesting_{i+1}].

Subtask 4. The solution for the last subtask naturally emerges from the previous one. If the balance is positive for some x = interesting_i, it is required to add additional green edges until the balance is restored, and every green edge corresponds to a railway segment. Thus, for every x = interesting_i one needs to add \max(0, balance \times length) to the answer, where length stands for the distance to the next interesting point (interesting_{i+1}-interesting_i).

The last piece is to make the graph connected. In case the balance is zero we can connect interesting_i and interesting_{i+1} with two edges in both directions, paying the length of this segment. Now we need to solve an instance of the well-known MST (minimum spanning tree) problem.


Comments

There are no comments at the moment.