There's also an O(nlogn) solution based on some observations. Let A{i,j} be
the smallest possible tail out of all increasing subsequences of length j
using elements {a1, a2, a3, ..., ai}.
Observe that, for any particular i, A{i,1} < A{i,2} < ... < A{i,j}. This suggests that if we want the longest subsequence that ends with a i + 1, we only need to look for a j such that Ai,j < ai + 1 < = Ai,j + 1 and the length will be j + 1.
Notice that in this case, Ai + 1,j + 1 will be equal to ai + 1, and all Ai +1,k will be equal to Ai,k for k != (j + 1).
Furthermore, there is at most one difference between the set Ai and the set Ai + 1, which is caused by this search.
Since A is always ordered in increasing order, and the operation does not change this ordering, we can do a binary search for every single a1, a2,..., an.
i,j都是小字体,唉,凑合着看吧。