Editorial for Tall


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: Plasmatic

Subtask 1

For this subtask, we can just brute force all possible values of c since any value of c above 255 would have bits above 27 set. This would cause the answer to always be 0.

Time Complexity: O(Nmax{ai}) or similar

Subtask 2

This subtask is for any solutions that have the same observation as subtask 3 but are otherwise too slow.

Time Complexity: O(N3) or similar

Subtask 3

Observe that after applying aic over all ai, at least one element must be zero so that our MEX is non-zero. Thus, ai=c for some i so there are only N possible values that c can be. The MEX of a sequence can be computed in linear time, which is sufficient to pass this subtask.

Time Complexity: O(N2) or O(N2logN)

Full Solution

First, remove any duplicates in the sequence so all elements are unique.

Consider some bit 2k and the least significant k+1 bits of the values in the sequence. Divide the sequence by whether each value has the bit corresponding to 2k set, giving us two sets of values. Notice that depending on whether bit 2k is set in c or not, one of these will only contain values in the range [0,2k1] and the other will only contain values in the range [2k,2k+11]. Call these the lower and upper sets respectively.

Observe that if the lower set has 2k values, then the answer is at least 2k no matter what the lower bits of c are, so we can just compute the answer for the bit 2k1 using the upper set only, yielding us the lower bits of c. On the other hand, if the lower set does not have 2k values, then the MEX can be at most 2k1 so the values of the upper set do not matter since they are all at least 2k. Then, we can do the same as the previous case, except using the lower set instead.

In the worst case, we just need to compute the answer for bit 2k1 using the two sets of numbers individually, and can then use those answers to compute the final answer in O(1) time. Starting this search from the most significant bit yields us a divide and conquer solution.

Time Complexity: O(Nlogmax{ai})


Comments

There are no comments at the moment.