OROPS - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: raysh07
Tester: iceknight1093
Editorialist: iceknight1093

DIFFICULTY:

Simple

PREREQUISITES:

None

PROBLEM:

Given an array A, you can do the following:

  • Choose an index i (1 \le i \lt |A|), delete A_i and A_{i+1}, and insert A_i \mid A_{i+1} in their place.
    Here, \mid denotes the bitwise OR operation.

Find the minimum number of operations needed to make all the elements of the array equal.

EXPLANATION:

Since each operation deletes two elements but inserts their bitwise OR in their place, at any point of time, every remaining element will equal the bitwise OR of some subarray of the original array.

More formally, if we perform several operations and the current array is B = [B_1, \ldots, B_k], then there will exist k ranges [l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k] such that:

  • l_1 = 1 and r_k = N
  • l_i = r_{i-1} + 1 for each 2 \le i \le k
  • B_i = A_{l_i} \mid \ldots\mid A_{r_i} for each 1 \le i \le k
    That is, B_i equals the bitwise OR of the subarray from index l_i to r_i of A.

Further, observe that performing an operation doesn’t change the bitwise OR of the overall array; because we’re merging some elements together and that doesn’t lose any information.

So, if all elements have to be equal in the end, they all have to be equal to the bitwise OR of the array A - there’s no other choice!
This is because x\mid x = x for any x, so if all the elements are equal then their bitwise OR also equals that element.


Let M = A_1\mid A_2\mid\ldots\mid A_N denote the bitwise OR of the whole array.

We know that in the final array, we must have several copies of M.
Each operation reduces the array length by 1, so minimizing the number of operations is equivalent to maximizing the number of copies of M we can create.

Let’s look at the leftmost element.
It must be M, and it must come from a subarray starting at index 1, i.e. be of the form [1, i] for some i.

Let i_1 be the smallest index such that A_1\mid\ldots\mid A_{i_1} = M.
Note that any larger prefix than i_1 will also have a bitwise OR of M.
Now, it’s optimal to just take the subarray [1, i_1] as our first subarray.

To see why, observe that if we don’t, then we’ve taken a larger prefix - say [1, i] where i \gt i_1.
But then we can just take [1, i_1] instead and give the range [i_1+1, i] to the second subarray instead - this won’t affect the bitwise OR of the second subarray since it’s already M anyway.

Thus, it’s optimal to simply cut out the shortest prefix we possibly can.
This can then be repeated: starting from index i_1+1, find the smallest index i_2 such that the bitwise OR of the range [i_1+1, i_2] equals M, cut this subarray out and continue, and so on and so forth.

This greedy strategy can be easily implemented in \mathcal{O}(N) time by simply scanning through the array.

Note that in the end, you might end up with a small suffix of the array whose bitwise OR is \lt M.
This is fine - this suffix can just be merged with the last cut subarray.


In the end, if you managed to cut out K subarrays, it means the final array will have K copies of M.
Our initial length is N and final length is K, so that means we require N-K operations to reach this state.

TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    
    target = 0
    for x in a: target |= x
    
    ans = 0
    cur = 0
    for x in a:
        cur |= x
        if cur == target:
            ans += 1
            cur = 0
    print(n - ans)
3 Likes