Editorial for IOI '05 P3 - Mean Sequence


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.

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 s1,,sn+1 is nondecreasing, then fixing a single si fixes the entire sequence, given its mean sequence m1,,mn. Let us define the reflection operation r on integer a with respect to center c as follows: r(a,c)=b if and only if a+b2=c; that is, r(a,c)=2ca. If si is fixed, then si+1=r(si,mi) and si1=r(si,mi1), etc. Hence, there exist an infinite number of sequences s1,,sn+1 that have the given sequence m1,,mn as mean sequence — one for each choice of s1.

There is a finite number of nondecreasing sequences though. A simple upper bound on the number of possible nondecreasing sequences may be m2m1+1, which is the number of integers between m1 and m2 (inclusive). This is because s2 must satisfy m1s2m2. Indeed, if s2<m1 then s1>m2 and therefore s2<s1 so the sequence is not nondecreasing. Similarly, if s2>m2 then s3<m2 gives a sequence which is not nondecreasing either. This way we can construct a solution, which tests all the possible values of s2 lying between m1 and m2, and for each such s2 it computes the rest of the sequence and then checks if it is nondecreasing. Such a solution works in O(n(m2m1+1)) time and can be implemented with O(n) or better O(m2m1+1) memory complexity.

Optimal solution

Before we present the model solution, the following definition will be useful. Let us call a value v feasible for si in the given sequence, when there exists a nondecreasing sequence s1,,sn+1 with si=v and mean sequence m1,,mn. Now we need an important observation.

Observation 1 If, for some i, values a and b with ab are feasible for si, then every c in the interval [a,b] is feasible for si.

The actual number of nondecreasing sequences s1,,sn+1 can be obtained by generalizing the problem as follows. Given a nondecreasing sequence m1,,mn, determine the interval of feasible values for sn+1. The answer is then the size of that interval.

In the model solution, the interval of feasible values of sn+1 is computed inductively. We start with the case n=0. In this case, the interval of feasible values for s1 consists of all integers: (,+). Now let [a,b] be the interval for nondecreasing sequences s1,,sn+1 having mean sequence m1,,mn. Let us consider the mean sequence m1,,mn extended by a new element mn+1mn. This reduces the possible values for sn+1 to the interval [a,min(b,mn+1)], and hence the interval for sn+2 is the reflection of this interval, that is, [r(min(b,mn+1),mn+1),r(a,mn+1)]. We consider interval [a,b] as empty if a>b, and otherwise it contains ba+1 elements. This way we obtain O(n) time complexity and O(1) 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 sn+1. One idea would be to use binary search. Even though this can result in an algorithm with O(nlogn) time complexity, it will need O(n) memory, which is too much to pass the largest tests.


Comments

There are no comments at the moment.