Editorial for DMOPC '23 Contest 1 P3 - Colour Scheme
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
The intended solution is to build the array from left to right.
The problem is now, given a known prefix of the array, can you determine the next element?
We will represent a query on the interval  as 
.
Lets assume the first  elements of the array have already been determined, i.e 
 have been determined. To determine 
 we can first do a query on the interval 
. There are now two cases.
Case 1:  
If , it's clear that 
 will not have appeared yet, thus we know it is a new distinct element.
Case 2:  
This implies that  has already appeared among the first 
 elements, we just have to determine which one it is.
The key observation is that , i.e 
 is monotonically increasing as 
 decreases. Clearly this is true since the number of distinct elements can only increase as the left end decreases. This motivates a binary search solution. Let 
 be a sorted array consisting of the indices of the last occurence of every distinct element in 
. Observe that if 
 then 
. In fact for all 
 we will have 
 (why?). With this in mind we can simply determine the last index of 
 where the equality holds with binary search, this index will be equal to 
. After determining 
, update the array 
 so that it considers 
 as a last occurence of some element.
The query complexity of this algorithm is , where 
 is the number of distinct elements in the array. With proper implementation, this should fit within the 
 query limit. Partial points are meant for sub-optimal implementations of the full solution.
Time Complexity: 
Comments