Codeforces Round 642 Div 3
Problem D:
You have an array arr
containing N
integers, which are all initially 0
. You need to perform N
actions, and in the action you do the following:
- Choose the subarray with maximum length having all zeroes, if multiple choose one which starts earlier
- Let it be if length is odd, set otherwise,
Hint 1
-
You just need to implement the algorithm as stated, but need to think how to get the interval on which we need to perform the action easily!
-
As we will process an interval, we put a value in the middle of it, and the left and right intervals will have all zeroes. Next time, we need an interval with larger length, and if length is same, with smaller start index. Can you think of some data structure that can query this quickly for you?
Hint 2
- What about a priority queue (pq)?
- Push the interval as
{length, {start, end}}
pair. Initially, pq will have{n, {0, n - 1}}
as the first element. - Now, while pq is not empty, pop the element at the front (that should be one with max length and if multiple, with smallest start) so you need to define a custom comparator.
- Once you pop this interval, update the value at middle, and push the left and right intervals. There are some minute implementation details you need to take care of.
- Also, note that the maximum no. of intervals in pq will not be more than
N
, think about it!
Time Complexity
Implementation Details
- Read priority_queue docs to know more about custom comparators.
- In short, you can define a struct or class and overrride the () operator inside it.
- This comparator works different from the custom comparators of sort function, you return exactly the opposite value of what is required.
- Do check for validity of an interval before pushing to the priority queue. (start must be <= end)