UVa 10534 - Wavio Sequence

Summary
You have the sequence of positive integers. You must find the length of longest Wavio Sequence. The Wavio Sequence of length $$2n+1$$ is the sequence where first $$n+1$$ integers in this sequence makes a strictly increasing sequence and the last $$n+1$$ integers of this sequence makes a strictly decreasing sequence.

Explanation
For each integer you can find the length of a strictly increasing sequence which end with this integer, and the length of a strictly decreasing sequence which start with this element. But if you do this by Dynamic Programming you got TL.

So, to makes the algorithm efficient you can used RMQ or Interval tree. You must convert all sequence in range (0,MMAX). Example, if you have sequence $$10,4, 18, 9$$, then you convert this sequence in the sequence $$2,0,3,1$$.

Then, for each integer X in convert sequence you want to know, what is the length of strictly increasing sequence, which end of this element. Do it with query L=RMQ(0,X-1). And then change the array: Change(X,L+1).

Do the same with the reverse of this sequence (longest decreasing sequence is the longest increasing sequence when this sequence reverse).

Then for each element of the convert sequence look the length of Wavio Sequence when this element is the max element in this sequence.

Input
10 1 2 3 4 5 4 3 2 1 10 19 1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1 5 1 2 3 4 5

Output
9 9 1