Editorial for DMOPC '19 Contest 6 P5 - University Math


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.

Author: george_chen

In the first subtask, we observe that the answer is just the total boundary length of the one or two bars we are asked to draw.

Time complexity: \mathcal{O}(N+Q)

We observe that the histogram can be modelled as a graph. Let every lattice point be a node and every boundary be a bidirectional edge that connects its endpoints. Each node in our graph has degree two or three. Observe that any valid way of drawing corresponds to some path in this graph.

Suppose the optimal path is P. From the task statement, P clearly traverses every edge of our graph at least once. If it traverses some edge more than once, we simply add duplicate edges until P traverses every edge exactly once. Thus, we observe that P is essentially an Euler path and our problem is reduced to finding the set of edges with smallest sum of lengths to add that ensures our graph contains an Euler path. From the definition of an Euler path, our graph can have at most two vertices with odd degree.

For the second subtask, our graph has at most 16 vertices. We can simply keep a bitmask of which vertices have odd degree and transition by adding edges to our graph. Since N is very small, it's better to preprocess all \mathcal{O}(N^2) possible queries beforehand.

Time complexity: \mathcal{O}(N^32^{3N+1})

There is no intended solution for the third subtask.

Time complexity: \mathcal{O}(N+Q \log N)

For the fourth subtask, we will further simplify our graph. We claim that any chain can be shortened into a single edge.

Suppose we have a chain (a sequence of nodes of degree 2). Observe that it is never more optimal to traverse part of this chain from one endpoint, turn back, traverse the remaining edges from the other endpoint, and turn back than it is to just traverse the entire chain at once and then turn back. This is because the distance is 2*\textrm{length of chain} using either method. Thus, we can replace all chains with a single edge which is the sum of the lengths of the edges of the chain. Thus, our graph is comprised only of vertices with degree 3.

Observe that the graph looks like this:

Thus, we can perform a bitmask dp with dp[i][j][k] representing the minimum sum of edge lengths with i being the current bar, j being a bitmask of the parity of the degree of the previous two nodes, and k being the number of nodes before i that have odd degree. Our final answer is the min of any state at the right end of our query interval with k \le 2.

Time complexity: \mathcal{O}(NQ)

There is no intended solution for the fifth subtask.

Time complexity: \mathcal{O}(N)

For the final subtask, we store the dp transitions on a segment tree. In a node covering the range [l,r] on the segtree, we store the minimum distance dp[st][ed][inc] where st is the bitmask of the nodes of the (l-1)-th bar, ed is the bitmask of the nodes of the r-th bar, and inc is how many additional nodes with odd outdegree are created in this interval. Queries can be answered by following the state transitions inside the nodes of the query interval. Initializing and querying the segment tree have a fairly high constant but solutions with decent implementation will pass.

Time complexity: \mathcal{O}(N+Q \log N)

Another approach would be to perform divide and conquer on the queries. Split the array at its midpoint and compute the dp from the midpoint to every other element of the array. Then, all queries crossing the midpoint can be answered by merging the answer to two bitmask dp's. The remaining queries can be answered by recursively solving the left and right halves of the array.

Time complexity: \mathcal{O}(Q+N \log N)


Comments

There are no comments at the moment.