Fast and Longest Rollercoasters

For $k\geq 3$, a k-rollercoaster is a sequence of numbers whose every maximal contiguous subsequence, that is increasing or decreasing, has length at least $k$; $3$-rollercoasters are called simply rollercoasters. Given a sequence of distinct numbers, we are interested in computing its maximum-length (not necessarily contiguous) subsequence that is a $k$-rollercoaster. Biedl et al. [ICALP 2018] have shown that each sequence of $n$ distinct real numbers contains a rollercoaster of length at least $\lceil n/2\rceil$ for $n>7$, and that a longest rollercoaster contained in such a sequence can be computed in $O(n\log n)$-time. They have also shown that every sequence of $n\geq (k-1)^2+1$ distinct real numbers contains a $k$-rollercoaster of length at least $\frac{n}{2(k-1)}-\frac{3k}{2}$, and gave an $O(nk\log n)$-time algorithm computing a longest $k$-rollercoaster in a sequence of length $n$. In this paper, we give an $O(nk^2)$-time algorithm computing the length of a longest $k$-rollercoaster contained in a sequence of $n$ distinct real numbers; hence, for constant $k$, our algorithm computes the length of a longest $k$-rollercoaster in optimal linear time. The algorithm can be easily adapted to output the respective $k$-rollercoaster. In particular, this improves the results of Biedl et al. [ICALP 2018], by showing that a longest rollercoaster can be computed in optimal linear time. We also present an algorithm computing the length of a longest $k$-rollercoaster in $O(n \log^2 n)$-time, that is, subquadratic even for large values of $k\leq n$. Again, the rollercoaster can be easily retrieved. Finally, we show an $\Omega(n \log k)$ lower bound for the number of comparisons in any comparison-based algorithm computing the length of a longest $k$-rollercoaster.


Introduction
The mathematical study of patterns occurring in sequences of numbers is a rather old and well developed topic in combinatorics and algorithms on sequences. Within this topic, of a particularly high interest is the study of long increasing and decreasing (not necessarily contiguous) subsequences occurring in a sequence. For example, already in 1749, Euler defined the Eulerian polynomials, which are the generating function for the number of descents in permutations. Almost 200 years later, Erdős and Szekeres [9] proved the existence of an increasing or a decreasing subsequence of length at least a + 1 in a sequence of at least n = a 2 + 1 distinct reals.
k-rollercoaster of length at least n 2(k−1) − 3k 2 . From an algorithmic point of view, both previously mentioned results were constructive, leading to an O(n)-time (respectively O(n log k)) algorithm computing a long (but not necessarily a longest) rollercoaster (respectively, k-rollercoaster) contained in a sequence of n distinct numbers. A longest rollercoaster contained in such a sequence was computed by an extension of Fredman's algorithm in O(n log n)-time, and if the input sequence is a permutation of {1, . . . , n} (or, more generally, sortable in linear time) in O(n log log n) time. By further generalising this approach, an O(nk log n)-time (respectively, O(nk log log n)-time) algorithm computing a longest k-rollercoaster in a sequence of n distinct numbers (respectively, a permutation of {1, . . . , n}) can be obtained. Note that, by the theorem of Erdös and Szekeres, a sequence of n distinct numbers always contains a √ n -rollercoaster, and the aforementioned algorithm computes a longest such rollercoaster in O(n 1.5 log n) time.
Our contributions. We consider the problem of computing a longest k-rollercoaster in an input sequence S[1 : n] and provide three results.
Firstly, we design a comparison-based algorithm computing the length of a longest krollercoaster in a sequence of n distinct numbers in O(nk 2 ) time. Thus, we obtain an optimal linear-time algorithm for constant values of k, in particular for k = 3. This significantly improves the results of [5] and shows that, even though longest rollercoasters are related to longest increasing subsequences, the rich combinatorial structure of the former makes them provably easier to find. The starting point of our algorithm is the following natural dynamic programming formulation. For each 2 ≤ i ≤ k, and for each element S[j], we compute a longest (not necessarily contiguous) subsequence of S ending with S[j] and with every run of length at least k, except for the last run, which has only i elements if i < k and at least k elements if i = k. Now the difficulty is to find the predecessor S[j ] of S[j] in such a subsequence in time proportional to k, in particular avoiding any kind of binary search. We greedily decompose the input sequence into blocks with a certain property related to Dilworth's theorem and prove, by a careful case analysis, that j must belong to the previous few such blocks. This, together with the special structure of the blocks and appropriate data structures, allows us to find j in O(k) amortised time.
Secondly, we focus on the case of large k. Given that both the previous and the new algorithm have at least linear dependency on k, it might seem plausible that this is inherent to the problem, for example that for k ≥ √ n the running time of any algorithm needs to be Ω(n 1.5 ). We show that this is not the case by designing a subquadratic algorithm that computes a longest k-rollercoaster in a sequence of n distinct numbers in O(n log 2 n) time. To obtain this result, we exploit the fact that if an increasing (respectively, decreasing) run in a longest k-rollercoaster extends from S[i] to S[j], then that run should be a LIS (respectively, longest decreasing sequence, LDS for short) in S[i : j]. If one arranges the length of LIS (respectively, LDS) in S[i : j] in an n × n matrix then the matrix has the anti-Monge property. It is known that all row maxima of an anti-Monge matrix can be found in O(n) time [2], that is, in sublinear time w.r.t. the size of the matrix (given an oracle access to the elements of the matrix). Such properties have been successfully exploited to speed up certain dynamic programming algorithms. We also follow this route, and construct a longest k-rollercoaster using dynamic programming, essentially by gluing together LISs and LDSs of consecutive contiguous subsequences of S.
Thirdly, we show that any comparison-based algorithm computing a longest k-rollercoaster needs Ω(n log k) comparisons. This follows from a similar reasoning to the one used by Fredman to show that any comparison-based algorithm computing the LIS needs Ω(n log n) comparisons. We leave designing an O(n log k)-time algorithm as an interesting open problem.
The paper is organised as follows. After a series of preliminaries, we describe the O(nk 2 )-time algorithm for computing the length of a longest k-rollercoaster, followed by the O(n log 2 n)-time algorithm. We conclude with the lower bound for the number of comparisons needed to compute the length of a longest k-rollercoaster in a sequence of length n.

Preliminaries
We consider sequences of distinct real numbers and work in the comparison-based model. If S is a sequence of n numbers, then |S| = n is the length of the sequence, and S[i] denotes its i th element. A subsequence of S is a sequence (S i 1 , S i 2 , . . . , S im ), defined by specifying the indices . A longest increasing (respectively, decreasing) sequence, for short LIS (respectively, LDS), is an increasing (respectively, decreasing) sequence with the largest possible length. Fredman gave an O(n log n)-time algorithm for computing the length of LIS, denoted res in Algorithm 1. A byproduct of this algorithm is a partition of S[1 : n] into res non-increasing subsequences that can be obtained by creating, for every 1 ≤ j ≤ res, a list of elements that has been stored in R[j]. res ← max{res, k + 1}

7: return res
A run in a sequence of numbers is a maximal contiguous subsequence that is increasing or decreasing. A k-rollercoaster is a sequence of numbers such that every run has length at least k; 3-rollercoasters are called, for short, rollercoasters. Given a sequence S[1 : n] we are interested in finding its longest subsequence that is a k-rollercoaster. To make the exposition easier to follow, we focus on finding the length of such a subsequence. Recovering the subsequence itself is, in all our algorithms, straightforward due to the dynamic programming approach we use; a brief description is given in Section 5.
3 Computing a Longest k-Rollercoaster in O(nk 2 )-Time In this section we show how to find a longest k-rollercoaster of S[1 : n] in O(nk 2 ) time. We begin our algorithm with a preprocessing phase. An alternating k-decomposition of S[1 : n] is a partition of S[1 : n] into contiguous subsequences (called parts) S 1 , S 2 , . . . , S m such that the length of LIS in the odd parts (S 1 , S 3 , S 5 , and so on) is k while the length of LDS in the even parts is k, possibly smaller for the very last part, and additionally removing the last element of any odd (even) part decreases the length of its LIS (LDS). In other words, for ≥ 1, S is the shortest contiguous sequence of S that follows directly after S 1 · · · S −1 and has for odd (even) a LIS (respectively, LDS) of length k. For example, an alternating 3-decomposition of S = (1,4,2,5,8,7,6,3) is (1,4,2,5), (8,7,6), (3). Proof. By terminating Algorithm 1 as soon as res = k we can find the shortest prefix of S with LIS equal to k in O(d log k) time, where d is the length of the prefix. Then we find the shortest prefix of the remaining suffix of S with LDS equal to k, and repeat. Overall, this takes O(n log k) time because all parts are disjoint. Proposition 1. Let A be a k-rollercoaster in S. Any part S contains elements of at most four consecutive runs of A.
Proof. By contradiction. Let S be S without the last element. If S contains elements of five consecutive runs of A then S contains elements of four consecutive runs of A, and hence all elements of two such consecutive runs. Thus, if S is an odd (even) part then S contains LIS (LDS) of length k, which contradicts the definition of an alternating k-decomposition.
By Dilworth's theorem, a part with LIS of length k can be decomposed into k decreasing subsequences, and such a decomposition can be obtained as a byproduct of Algorithm 1. Thus, we can decompose each part into up to k monotone (increasing or decreasing, depending on whether the part is odd or even) subsequences. These subsequences can be then merged to obtain a sorted list P of all elements in the corresponding part S in O(n log k) overall time, for example by first merging pairs of subsequences, then quadruples, and so on.
Before moving on to the description of our algorithm, we need a combinatorial lemma that relates an alternating k-decomposition to a longest rollercoaster.

Lemma 3.
Suppose that x = S[j] is an element occurring in an increasing run of a longest k-rollercoaster, and y = S[j ] is its predecessor in the same run, and consider an alternating k-decomposition of S[1 : n]. Then either x and y are in the same part S i , or y is in one of the parts S i−4 , S i−3 , S i−2 , S i−1 .
Proof. By contradiction. Suppose that there are at least four parts between x and y, i.e., x is in S i and y is in some S k with k < i − 4. Let r denote the run in the k-rollercoaster that contains x and y, let d be the length of r, and let be such that r[ ] = y and r[ + 1] = x. We assume that r is an increasing run (see Figure 2); the case when r is decreasing can be treated in the same way. Consider the following four cases: 1. ≤ k − 1 (i.e., there are at most k − 2 elements in r before y) and k − 2 ≥ d − − 1 (there are at most k − 2 elements in r after x).
Recall that there are at least four whole parts between x and y. Therefore, in particular there are three consecutive parts S i , S i +1 , and S i +2 such that the first has LIS of length k, the second has LDS of length k, and the third has LIS of length k.
In the first case, we replace r[2 : d − 1] with LIS of S i , the LDS of S i +1 , and LIS of S i +2 . It is straightforward to verify that we obtain a valid k-rollercoaster, and because we remove at most 2k − 1 elements and add at least 3k, this creates a longer k-rollercoaster, which is a contradiction. In the second case, we replace r[2 : ] with LIS of S i and LDS of S i +1 . Again, it is straightforward to verify that we obtain a valid longer k-rollercoaster, because we remove at most k − 2 elements and add at least 2k. Similarly, in the third case, we replace r[ + 1 : d − 1] with LDS of S i +1 and LIS of S i +2 to obtain a longer k-rollercoaster. Finally, in the fourth case we simply insert LDS of S i +1 between x and y to obtain a longer k-rollercoaster.
After the initial preprocessing phase we apply dynamic programming. For 1 ≤ i ≤ k, we say that a subsequence of S (not necessarily contiguous) is a (k, i) + -rollercoaster if it ends with an increasing run of length exactly i when i < k and at least k when i = k, while every other run is of length at least k. We want to construct, for every 1 ≤ i ≤ k and 1 ≤ j ≤ n, a longest Merge the k monotone subsequences constituting S to obtain a single sorted list P .
for 1 ≤ d ≤ 4 and each S[j] ∈ S in the order of their occurrences in P do 7: for each S[j] ∈ S in the order of their occurrences in P do

18:
Find M + [j, i] and L + [j, i] using decomposition of S into k monotone sequences.

21:
Update M + [j, i] so that it corresponds to L + [j, i].

22:
Update, for each S[j] ∈ S , L − [j, i] and M − [j, i] with a similar approach.
When we begin computing the arrays We start with computing the values in its corresponding rollercoaster belongs to S −d , for some 1 ≤ d ≤ 4. In such case the longest rollercoaster ending at S[j ] has been already correctly determined and the computation is quite straightforward. If S[j ] also belongs to S , we must be more careful to guarantee that the longest rollercoaster ending at S[j ] is already known. We proceed in iterations. In the t th iteration, we guarantee to compute the values such that at most t runs of the corresponding rollercoaster contain elements from S . By Proposition 1, four iterations are enough. In a single iteration, we start with computing the initial values M + [·, 1], L + [·, 1], M − [·, 1] and L − [·, 1] corresponding to S[j] being the first element of its run. These values can be simply copied from the already known M − [·, k], L − [·, k], M + [·, k] and L + [·, k] corresponding to S[j] being the last element of a rollercoaster with less than t runs containing elements from S (or set to 1 corresponding to S[j] being the only element in the rollercoaster). This is correct because a (k, 1) + -rollercoaster is actually either a (k, k) − -rollercoaster or a sequence consisting of a single element. Then, we calculate the values M + [·, i], L + [·, i], M − [·, i] and L − [·, i], for 2 ≤ i < k, such that the predecessor S[j ] ∈ S belongs to the same run as S[j]. By performing the calculation for i = 2, 3 . . . , k − 1 in this order we guarantee that the longest rollercoaster ending at the predecessor S[j ] ∈ S is already known for all S[j] ∈ S , but the computation is still not completely trivial and requires a different approach depending on whether S was decomposed into at most k increasing subsequences or at most k decreasing subsequences. Finally, we extend this to i = k.
The idea is to compute these candidates in the order in which the elements S[j] occur on the sorted list P . So, let us consider P and P −d . For each element S[j] in the current part we want to identify a longest (k, i − 1) + -rollercoaster ending in S −d with an element less than S[j]. Thus, as P −d is increasing, for every element of the current part we need to consider all elements in a prefix of is maintained as we move from left to right in P −d . Then we proceed to the next element in P . Overall, computing candidates M d As in the previous case, we can use two pointers to sweep through I a and I b and compute, for M + [j, i] belongs to S decomposed into k decreasing subsequences. This is the most complicated case. Recall that the decomposition into k decreasing subsequences D 1 , D 2 , . . . , D k was obtained with Algorithm 1. In more detail, D a consists of elements assigned to R[a] throughout the execution of the algorithm. Thus, if S[j] ∈ D a then the predecessor of S[j] in a sought longest (k, i) + -rollercoaster, denoted S[j ], must belong to D b for some 1 ≤ b < a. Consider an element S[j] ∈ D a and 1 ≤ b < a. The elements of D b that can be the predecessor of S[j] in a (k, i) + -rollercoaster (that is, possible candidates for M + [j, i]) are both less w.r.t. value and w.r.t. position in S, similarly as in the previous case. The difference is that now these elements form contiguous subsequence X of D b that is not necessarily a prefix. The first element of X can be found by searching for the first element with sufficiently small value, while its last element can be found by searching the last element with sufficiently small position (note that X might be empty). Let S[j ] be the next element after S[j] in D a , and Y be its corresponding contiguous subsequence of D b consisting of possible predecessors in a (k, i) + -rollercoaster. Clearly, S[j] > S[j ] while j < j . Thus, the first element of Y is either the same as the first element of X or occurs after the first element of X in D a , while the last element of Y is either the same as the last element of X or occurs after the last element of X in D a . Thus, we sweep through D a while maintaining the current contiguous subsequence X of D b corresponding to the possible predecessors of the current S[j] ∈ D a . This requires the following tool. The reason why this works is that, due to the order in which we consider the elements of S , at the moment when we compute the length of a longest (k, k) + -rollercoaster ending with S[j], and which may have more than k elements in the final run, we have already computed the length of a longest (k, k) + -rollercoaster ending with any element S[j ] which may be a predecessor of S[j] on the respective (k, k) + -rollercoaster.
Conclusion. With these final remarks, our algorithm is completely described. It only remains to find the element S[j] for which max{L − [j, k], L + [j, k]} is maximum. The correctness follows from the comments made throughout its description. To compute the complexity, it is enough to note that each part S of the partition of S is processed in O(k|S |) time, for each 2 ≤ i ≤ k. Adding this up, we get that the total complexity of our algorithm is O(nk 2 ). Before we describe our algorithm, we introduce two preliminary procedures. Firstly, we introduce the definition of an anti-Monge matrix and the algorithm for finding the maximum in every column of such a matrix. Secondly, we describe the algorithm for finding LIS in contiguous subsequences of the input sequence. Finally, we describe the algorithm computing a longest k-rollercoaster in the input sequence, using the previously developed tools as black boxes.
Monge matrices. Let A be an n × n matrix, and A[i, j] denote its element in the i th row from the top and the j th column from the left. A is Monge (respectively, anti-Monge) if, for every 1 ≤ i < j ≤ n and 1 ≤ k < ≤ n, the Monge equality holds, namely ). An n × n falling staircase anti-Monge matrix is a matrix with blanks such that for every blank all elements below and to the left are blanks, and the anti-Monge inequality holds whenever the four concerned elements are non-blank. Similarly, an n × n reverse falling staircase anti-Monge matrix is a matrix with blanks such that for every blank all elements above and to the right are blanks, and the anti-Monge inequality holds whenever the four concerned elements are non-blank. Finally, an n × n matrix A is totally monotone if, for every 1 ≤ i < j ≤ n and 1 ≤ k < ≤ n, The following lemma follows from the well-known SMAWK algorithm [2].

Lemma 3 (Lemma 3.3 in Aggarwal et al. [1]
). All row maxima in a reverse falling staircase totally monotone matrix can be found in O(n) time.
By transposing the matrix and observing that being anti-Monge implies being totally monotone we obtain the following. LIS-in-range queries. Let S[1 : n] be the input sequence. Define M as an (n + 1) × (n + 1) matrix with 0-indexed rows and columns, such that M [i, j] is the length of LIS in S[i + 1 : j] for i < j and M [i, j] = j − i otherwise (the anti-Monge matrix in Figure 3 is such a matrix for the sequence (3, 4, 1, 2)). As hinted by our example, this matrix turns out to have a rather special structure as observed by Tiskin [19]. We describe this structure in the following.
Let S be the sequence obtained by sorting S (recall that S consists of distinct elements), and observe that LIS of S is the same as a longest common sequence (LCS, for short) of S and S . Thus, we can think that M [i, j] is LCS of S and S[i : j]. As such, the following result can be shown (see [19] and the references therein).

Lemma 5. M is anti-Monge.
Our algorithm needs to access the elements of M . Since the matrix contains (n + 1) 2 elements, it is too large to be explicitly stored in memory. Fortunately, Tiskin also showed how to create in O(n log 2 n) time an O(n)-space implicit representation of M that allows us to obtain any of its elements in O(log n) time [19]. Before we present the internals of this representation, we need to introduce some additional definitions illustrated in Figure 4. Definition 1. Let A be any n × n matrix. Its distribution matrix A Σ is an (n + 1) × (n + 1) matrix defined by A Σ [x, y] = i>x,j<y A[i, j], for every 1 ≤ x ≤ n + 1, 1 ≤ y ≤ n + 1.

Definition 2.
A permutation matrix is a square matrix that has exactly one 1 in every row and column, and the remaining elements are equal to 0.  Proof. Let A be the sequence consisting of all elements from both A and r. Recall that a sequence is a k-rollercoaster if every run has length at least k. We need to consider three cases: the first element of r is the last element of A, the first element of r is greater than the last element of A, and the first element of r is less than the last element of A.
In the first case, all runs in A but the last are the same as in A, and the last run is equal to r. Since A is a k-rollercoaster and |r| ≥ k we conclude that A is a k-rollercoaster. A and r have one common element, so |A | = |A| + |r| − 1.
In the second case, all runs in A but the last are also the same as in A, and the last run consists of the last element of A and r. Again we conclude that A is a k-rollercoaster. Since A and r have no common elements, |A | = |A| + |r|.
In the third case, all runs in A but the last two are the same as in A. The second-to-last run in A consist of the last run of A and the first element of r, and the last run in A is r. We conclude that A is a k-rollercoaster. Since A and r have no common elements, |A | = |A| + |r|. Proposition 2 cannot be applied directly if we aim to achieve the announced O(n log 2 n) time complexity, and we need to introduce some auxiliary definitions. For every 1 ≤ d ≤ x we define inc d [x] as follows:  Proof. By Lemma 5 M is an anti-Monge matrix. By Observation 1 this is still the case if we add the same value to all elements in the same row.
To prove that A is a falling staircase matrix consider a non-blank element A[i, j]. Then M [i, j] ≥ k. But this implies M [i − 1, j] ≥ k and M [i, j + 1] ≥ k (as long as i > 1 and j < n), so all elements above and to the right are also non-blank as required.  We can now state with the main result of this section.

Constructing a Longest k-Rollercoaster
In this section we briefly discuss how to construct a longest rollercoaster for both algorithms.
For the O(nk 2 ) algorithm. In the respective algorithm, for each 2 ≤ i ≤ k, and for each element S[j], we compute the predecessor of S[j] on a longest (not necessarily contiguous) subsequence of S ending with S[j] and with every run of length at least k, except for the last run, which has only i elements if i < k and at least k elements if i = k. If, together with this predecessor, we store also the length of the of the last run in the respective subsequence of S, we can trace the whole sequence back. Indeed, the predecessor gives us the information what element should we list before S[j] in the subsequence. The length of the run gives us information on the length of the run ending with the predecessor of S[j], so we know where we should look in our data structures for the predecessor of the predecessor of S[j]. For some i and j, tracing back a longest (not necessarily contiguous) subsequence of S ending with S[j] and with every run of length at least k, except for the last run, which has only i elements if i < k and at least k elements if i = k, takes, clearly, O(n) time, provided that we have the information described above.
In the end, we will only need to trace back a longest (not necessarily contiguous) subsequence of S ending with some element S[j] and with every run of length at least k. Given that we also compute the length of a longest (not necessarily contiguous) subsequence of S ending with each S[j] and with every run of length at least k, we can select in O(n) the ending element of the subsequence we need to trace back.
In conclusion, once the O(nk 2 ) algorithm for computing the length of a longest k-rollercoaster is executed, we can actually compute the respective k-rollercoaster in O(n) additional time.
For the O(n log 2 n) algorithm. In order to retrieve the elements of a longest k-rollercoaster we need to extend our algorithm to maintain global arrays P red inc [1, . . . , n] and P red dec [1, . . . , n]. Elements of these arrays are computed during the calculations of inc m [m : j] and dec m [m : j] as follows. Initially they are equal to −1. After execution of the algorithm we demand that P red inc satisfies following: for every 1 ≤ i ≤ n we have that inc i] otherwise, and similarly for P red dec . It is straightforward to augment the algorithm from Corollary 1 to obtain such information.
We retrieve the elements of a longest k-rollercoaster from the last one to the first one. Recall that a longest k-rollercoaster has the length equal to max{inc[n], dec[n]}. We focus on how to obtain a longest k-rollercoaster R of length inc[n] with last run increasing (so, assume, w.l.o.g., that inc[n] > dec[n]); the procedure is similar for dec[n] and the last run decreasing.
Observe that if inc[n] is equal to the length of LIS in the input sequence, we can obtain the elements of R by Adding up the time needed to compute LIS or LDS for each of these ranges we get O(n log n) total time needed to obtain elements of a longest k-rollercoaster.

Lower Bound
In the final section of our paper, we prove that any comparison-based algorithm computing the length of a longest k-rollercoaster in an permutation S of {1, . . . , n}, for 4 ≤ k ≤ n 3 , performs at least Ω(n log k) comparisons. Let T be a binary comparison tree associated with an algorithm that computes the result. The number of comparisons made in the algorithm is equal to the height of T , and this is a lower bound on the execution time of the algorithm.
Let A be a partial ordering associated with a path from the root to some leaf of T . Since the algorithm cannot distinguish between permutations following the same path, every permutation consistent with A has to give the same result. Our approach is to first identify a set U of permutations of {1, . . . , n} such that log |U | = Θ(n log k), and any ordering associated with a leaf of T can be consistent with at most one permutation from U . Hence, the number of leaves in T is at least |U |. Since the height of a binary tree is at least logarithm of the number of leaves, this will show that the height of T , and hence also the number of comparison performed by the algorithm, is at least Ω(log |U |) = Ω(n log k).
We first recall the set Γ of n−2 permutations of {1, . . . , n} proposed by Fredman in [10], where is a parameter. These permutations are essentially different inputs S for an algorithm computing the length of LIS, each leading to a different leaf in the comparison tree associated to that algorithm.
So, essentially, we want to construct input sequences (x 1 , . . . , x n ), with their elements x 1 , . . . , x n chosen so that certain linear orderings of the x i s are induced. To create a permutation from Γ we partition (x 1 , . . . , x n ) into subsequences P 1 , P 2 , . . . , P . To simplify the exposure, the first elements of a sequence are the ones in its prefix of length , while the last elements are the ones in its suffix of length ; the remaining n − 2 elements are called the middle of the sequence. We partition (x 1 , . . . , x n ) in the following way: the i th element of the first part (x i ) and the i th element of the last part (x n− +i ) belong to P i . Each element from the middle of the sequence belongs to an arbitrary chosen part P j . This gives us n−2 different partitions. For a partition P 1 , . . . , P , we assign values from {1, . . . , n} to the input sequence in such a way, that the elements of each part P i form a decreasing sequence and, for 1 i ≤ , each element of P i is less than any element of P i+1 (see Figure 5). So, each such possible assignment gives us a permutation from Γ. LIS of any permutation from Γ is of length because it contains one element from each P i . LDS of any permutation of Γ is no longer than n − 2 + 2 because it contains at most one element from the first elements and at most one from the last .
Proposition 5. Each permutation from Γ can be split into descending subsequences in only one way. For two different permutations from Γ these ways of splitting are different.
Proof. Let P be a permutation from Γ and P 1 , . . . , P be its corresponding partition as described above. Observe that the first elements form an increasing subsequence, so no two of them can be in the same decreasing subsequence. The same holds for the last elements. Now let D 1 , . . . , D be a partition of P into decreasing subsequences, such that that D i contains the i th element from the first part. Since the last elements form an increasing subsequence, each D i has to contain exactly one of them. Because only the first element in the last part is smaller than the first element in the first part, D 1 actually has to contain the first element of the last part. By repeating this reasoning we obtain that D i contains the i th element from the first part and also the i th element from the last part. Then, we obtain that D 1 is actually equal to P 1 , and by repeating this reasoning we conclude that D i = P i for all i = 1, . . . , .
We now consider the algorithm computing the length of a longest k-rollercoaster. Using the permutations from Γ we create a set U of k n k−3 3k−3 permutations of {1, . . . , n}, again with the same principle behind: they should be input sequences which lead to different paths in the comparison tree associated to an algorithm computing the length of a longest k-rollercoaster. Observe that log (k n k−3 3k−3 ) = Θ(n log k), so this would imply the desired lower bound of Θ(n log k) on the number of comparisons done by an algorithm to compute the length of a longest k-rollercoaster.
A permutation from U is obtained as follows. Suppose that (3k − 3) divides n. Split the sequence (x 1 , . . . , x n ) into n 3k−3 blocks (contiguous subsequences) of size 3k − 3. We will assign to the elements of the i th contiguous block (x i(3k−3)+1 , . . . , x (i+1)(3k−3) ) distinct values from the set {i(3k − 3) + 1, . . . , (i + 1)(3k − 3)}, as follows. In every block, use one of the permutations from Γ (with the parameter set to k) to values to the elements x i(3k−3)+1 , . . . , x (i+1)(3k−3) of that block, and then assign values to those elements according to that ordering. In this way, we can create |Γ| n 3k−3 = (k k−3 ) n 3k−3 permutations of {1, . . . , n}. Observe that in every block the length of a longest decreasing subsequence is less than k. Since every block consists of strictly greater values than the previous ones, a longest decreasing subsequence of every permutation from U is less than k. A longest increasing subsequence of every element of Γ is equal to k, so a longest k-rollercoaster for every element of U is equal to kn 3k−3 and consists only of longest increasing subsequences corresponding to all the blocks glued one after the other. We can now show a result similar to Proposition 5.

Elements corresponding to P2
Elements corresponding to P1 Elements corresponding to P3
Proposition 6. Each permutation from U can be split into kn 3k−3 descending subsequence in only one way. For two different permutations from U these ways of splitting are different.
Proof. Let S be a permutation from U . Recall that we can partition S into n 3k−3 contiguous blocks of length 3k − 3. All values in a block are strictly greater than the values in all previous blocks, so in a decreasing subsequence of S we can have only elements from one block. Since every block corresponds to a permutation from Γ, by Proposition 5 it can be split into exactly k decreasing subsequences in only one way. For each two different permutations of U , there exists at least one block (i.e., permutation from Γ) that differentiates them. By Proposition 5, this block is split in a different way than all the other blocks of Γ, so the conclusion follows: each particular permutation from U will also be split in a different way than all other permutations of U .
Having constructed the set U , we can proceed with the lower bound. Let A be a partial ordering associated with a path to some leaf of T (the comparison tree associated to the algorithm computing the length of a longest k-rollercoaster). Since the algorithm cannot distinguish between permutations following the same path, every permutation consistent with A has to give the same result.
We recall the following lemma.
Lemma 5 (Lemma 3.6 in [10]). Let ≤ be a partial ordering defined on S. The maximum length of LIS in S associated with any linear embedding of this ordering, is equal to the minimum number of decreasing subsequences relative to ≤ into which S can be partitioned.
Now we can prove the following.
Lemma 6. Let A be partial ordering associated with the path from the root to a leaf of T . Only one permutation from U can be consistent with A.
Proof. Consider S ∈ U that is consistent with A, and let D = kn 3k−3 be the length of its LIS. Now let m be the minimum number of decreasing subsequences relative to the results of the comparisons made on the path A into which S can be partitioned. If m < d then S is consistent with A, so we can partition S into the same decreasing subsequences, but S cannot be divided into less than than d decreasing subsequences, a contradiction. If m > d then by Lemma 5 there exists a permutation S consistent with A with the length of LIS greater than d. S follows the same path as S in the comparison tree, but has a longer k-rollercoaster (consisting only of LIS of S ) than S, a contradiction. Thus, m = d for any such S. Now consider two S 1 , S 2 ∈ U consistent with A. By Proposition 6 it follows that the only partition of S 1 into d decreasing sequences is different from the only such partition of S 2 (also into d decreasing sequences), so A can be consistent with only one permutation, a contradiction.
Thus, each permutation from U corresponds to a distinct leaf of T , making the depth of T at least log |U | = Θ(n log k) as required and proving the following theorem.
Theorem 9. For every k satisfying 4 ≤ k ≤ n 3 , any comparison-based algorithm that computes the length of a longest k-rollercoaster in a permutation of {1, . . . , n} performs at least Ω(n log k) comparisons.