Editorial for IOI '05 P3 - Mean Sequence
Submitting an official solution before solving the problem yourself is a bannable offence.
At first, observe that the definition of mean sequence can be applied to any sequence, not only nondecreasing sequences. If we drop the condition that sequence is nondecreasing, then fixing a single fixes the entire sequence, given its mean sequence . Let us define the reflection operation on integer with respect to center as follows: if and only if ; that is, . If is fixed, then and , etc. Hence, there exist an infinite number of sequences that have the given sequence as mean sequence — one for each choice of .
There is a finite number of nondecreasing sequences though. A simple upper bound on the number of possible nondecreasing sequences may be , which is the number of integers between and (inclusive). This is because must satisfy . Indeed, if then and therefore so the sequence is not nondecreasing. Similarly, if then gives a sequence which is not nondecreasing either. This way we can construct a solution, which tests all the possible values of lying between and , and for each such it computes the rest of the sequence and then checks if it is nondecreasing. Such a solution works in time and can be implemented with or better memory complexity.
Optimal solution
Before we present the model solution, the following definition will be useful. Let us call a value feasible for in the given sequence, when there exists a nondecreasing sequence with and mean sequence . Now we need an important observation.
Observation 1 If, for some , values and with are feasible for , then every in the interval is feasible for .
The actual number of nondecreasing sequences can be obtained by generalizing the problem as follows. Given a nondecreasing sequence , determine the interval of feasible values for . The answer is then the size of that interval.
In the model solution, the interval of feasible values of is computed inductively. We start with the case . In this case, the interval of feasible values for consists of all integers: . Now let be the interval for nondecreasing sequences having mean sequence . Let us consider the mean sequence extended by a new element . This reduces the possible values for to the interval , and hence the interval for is the reflection of this interval, that is, . We consider interval as empty if , and otherwise it contains elements. This way we obtain time complexity and memory complexity solution, because there is no need to store the entire sequence in memory. Intervals of feasible values can be computed while reading the input data.
There are also some suboptimal solutions, which use different methods of determining the interval of feasible values for . One idea would be to use binary search. Even though this can result in an algorithm with time complexity, it will need memory, which is too much to pass the largest tests.
Comments