very dp heavy contest, i think there was something of a theme running there under dp as well.
Problem A
We can think of each line segment as an independent problem. In particular, if a line segment has length l, then it takes ceil(l/2) operations to clear. Hence, max_i(ceil(li/2)) is the answer.
Problem B
The greediest strategy is to try to conform the list to 1, 2, 3, 4, .... In particular, any books which are extra to this pattern can be bumped to later stacks. As long as there are sufficient books to form the pattern, the answer is yes, otherwise no.
Problem C
We can form a partition over the elements over the array based on which are reachable from each other. We know that this forms a partition because reachability forms an equivalent class, in particular
- Reflexive: any element is reachable from itself
- Symmetric: if element A is reachable from element B, then element B is reachable from element A (by using the same jumps)
- Transitive: if element A is reachable from element B, and element B is reachable from element C, then element A is reachable from element C
Hence, we perform disjoint set unions to join each element at index i, with elements at index i + x and i + y. (this also joins elements j to elements j - x and j-y)
Problem D
This problem requires bashing a few sums, so bear with me.
Suppose there was a post with impact value . The total productivity is However, by taking the post with impact value , the total productivity is This shows that the negative sign carries to the outside of the sum (linearity of mult by -1).
Now, let's suppose we also want to take post with . The total productivity is We can visualize this as the range getting double flipped. In general, we can imagine partitioning into ranges . These ranges can be arbitrarily flipped through taking some motivational posts.
The best strategy is to take the posts which makes each of the ranges maximal. In particular, the sum of the range can either be for which A key insight is that we actually don't need to determine which posts to take, we can simply reason about the impact of taking the optimal set of posts. Also note that the last range is always as is.
Problem E
This problem builds on many of the ideas in Problem D.
In order for the final string to be alternating,
A key insight is that flipping a given range does not affect the alternating nature of that range. Hence, a flip of a range is only used to modify the alternating nature of the ranges immediately adjacent (before and after) to it. Note that each range flip can be used to modify two seperate locations where the string is not alternating. It can also be used to modify a single location by flipping a range from 1 to the location. Hence, we can use a prefix sum array to keep track of for each range (between the given ) how many non-alternating locations exist (), then using flips to fix the non-alternating locations.
Problem F
The key to solving this problem is effectively visualizing the rotation operation.
Note that
- Relative order is preserved in rotation
- Rotating parents do not affect the relative order of their children
Hence, the only way to have a valid ordering is if
- The children of a node can be made to be ordered
- The ranges of the higher nodes are disjoint and ordered (specifically if one node has, for example, children 1, 4 and the other has children 2, 5 this is not sortable)
Hence, a simple DFS, while keeping track of the ranges of the children, is sufficient to solve this problem.
Problem G
This is a DP problem, with dp transition as In particular, is the maximum number of books which can be collected if we take index (and end there). This might at first look like an dp. Two mechanisms allow this to be .
First, we need some mechanism to query This is achiveable with a max fenwick tree.
Next, we need some mechanism to introduce values into the fenwick tree when they are allowed to be queried. Specifically, this is to achieve the part of the max constraints. We need to introduce into the fenwick tree at index . This is achiveable with a priority queue. While traversing through the array, we can pop from the priority queue all elements which are allowed to be queried, and introduce them into the fenwick tree.