Editorial for COCI '15 Contest 3 #5 Nekameleoni
Submitting an official solution before solving the problem yourself is a bannable offence.
We will describe a "divide and conquer" algorithm that, given an array of length , finds the shortest subsequence that contains all numbers from to .
solve(T):
L = left half of array T
R = right half of array T
X = solve(L)
Y = solve(R)
Z = the shortest subsequence that contains all numbers from 1 to K,
and is comprised of the suffix of array L and the prefix of array R
return min(X, Y, Z)
If we can calculate the value in the complexity , then the algorithm's complexity is .
How to determine the value ? For each suffix of the array , we will calculate a bitmask that describes a set of numbers that is in that suffix ( so that bitmask will be a single long long
). We will do the same thing for each prefix of array . Now we want to find two bitmasks whose binary or is equal to , and the sum of the lengths of the corresponding suffix and prefix is minimal. Notice that there are at most interesting bitmasks from the left and from the right side, so we can do this in by trying out all pairs of bitmasks. We can use the "monotonous" property of the bitmasks so we can achieve the same result in .
Nevertheless, this is not quick enough because we have queries that need to be answered.
We will construct a tournament tree where each node stores "interesting" sets of bitmasks for prefixes and suffixes of its interval and the shortest subsequence that contains all numbers from to in its interval. Then the merging of nodes in the tree actually corresponds to the step of the aforementioned algorithm. When we modify a number in the array, we need to recalculate the values in nodes, so the complexity of a single change is , and the total complexity is .
Comments