UVa 10534 - Wavio Sequence

UVa 10534 Wavio Sequence
http://acm.uva.es/p/v105/10534.html

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.

Implementations
Notes/Hints on actual implementation here.

Optimizations
Optimizations here.

Input
Input Here

Output
Output Here