## Editorial for IOI '19 P6 - Sky Walking

**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

For each skywalk and each building, check if they intersect and if they do, add a new vertex for the intersection point. Additionally, put a node on the bottom of each of the buildings , and . Add edges between consecutive nodes on each building and also for consecutive nodes on each skywalk. Use Dijkstra to find the shortest path from the node on the bottom of building to the node at the bottom of building . The number of vertices and edges are , so the total complexity is .

#### Subtask 2

The solution for the previous subtask works here as well, however the graph must be built more efficiently. To do that, iterate in increasing order over heights which either contain a skywalk or the endpoint of a building. The goal is to keep a list of all buildings that are at least as tall as the current height. Given such a list, for each skywalk, start from its left endpoint and move through the list. Each element is an intersection between that skywalk and a building. Hence you will at most visit elements before reaching the right endpoint. Add a vertex for each intersection. The rest of the solution is the same as the last subtask. For maintaining the list of buildings, it is enough to start from a list of all buildings. Then at each height, after processing the skywalks for that height, remove the buildings that have an endpoint at that height. This can be done using a linked list or a BST.

#### Subtask 3

When and , it can be proven that skywalks are always traversed from left to right. Additionally, when all of the buildings have the same height, it can be shown that there exists an optimal path where each skywalk is either not visited or is traversed completely until its right endpoint (though the entrance point to that skywalk might not be its left endpoint). Iterate over skywalks in increasing order of their right endpoint, breaking ties in favor of lower skywalks. The goal is to maintain the minimum cost to reach each skywalk's right endpoint. During the iteration, for each skywalk, update this value for the skywalks just above, and just below its right endpoint based on its own value. The answer is the minimum cost to reach the right endpoint of one of the skywalks ending at , adding the cost of reaching the bottom of building .

#### Subtask 4

For this subtask, the solution is to use the same strategy as the first two subtasks and build a graph. Consider the graph in those two subtasks. Let be a vertical edge connecting points and for some , , (). Edge is called irrelevant if the point lies strictly inside of some skywalk. Since and , it can be proved that there exists a shortest path that does not pass through irrelevant edges. Hence, after removing the irrelevant edges from the graph, the length of the shortest path doesn't change. In the new graph, for each vertical edge, the top node is the endpoint of a skywalk. So, there are at most vertical edges left in the graph. This means at most vertices have at least one edge connected to them. Discarding the other vertices, the same approach as the first two subtasks can be followed on the new graph.

#### Subtask 5

The solution for this subtask is almost the same as the last subtask. However, for the previous theorem to hold, the skywalks must be adjusted. Specifically, for each skywalk between two buildings such as , and where , divide it into 3 parts as follows:

- let be the last building before (including ) that is as tall as this skywalk.
- let be the first building after (including ) that is as tall as this skywalk.
- replace the skywalk with the following skywalks:
- skywalk connecting buildings and .
- skywalk connecting buildings and .
- skywalk connecting buildings and .

The same adjustment must be done for all skywalks that intersect or pass over building . Then the same solution as subtask 4 works.

## Comments