Editorial for COCI '20 Contest 5 #5 Sjeckanje
Submitting an official solution before solving the problem yourself is a bannable offence.
The first subtask can be solved by dynamic programming. We find the largest possible value of each array prefix. When we are on some element, we try to cut the string in all possible places to the left, and maintain the current minimum and maximum. The complexity is
For other subtasks, we need the following observation: It is optimal to chop the array into monotonous (ascending or descending) segments. If the segment is not monotonous, we can always cut it into two parts that will have higher total value.
The value of a monotonous segment
Let
We have reduced the problem to the following: we are given an array of integers
For the second subtask, we can recompute the maximum possible value after each update by simple dynamic programming in complexity
We can solve the full problem using a segment tree. Each node remembers the maximum value of its segment, for each of the four cases of (not) taking the first and the last element. While merging, we have to pay attention to the signs of the elements on the border to be joined. Note that when an update occurs, at most two
Comments