[Compound] Longest increasing subsequence – LIQ

Threads and test: link

Given an integer sequence of N element A[1], The[2], … The[N].
Knowing that is monotonically increasing subsequence 1 Series A[i1],… The[I] satisfy
i1 < i2 < … < I và A[i1] < The[i2] < .. < The[I]. Please indicate monotonically increasing subsequence of the sequence length how many elements?
Download and test solution (C / C , Pascal) here.
Input
Current 1 including 1 integer is the number N (1 ≤ N ≤ 1000).
The first line 2 A recording N integers[1], The[2], .. The[N] (1 ≤ A[in] ≤ 10000).
Output
Remember that the length of monotonically increasing subsequence longest.
Example
Input:
6
1 2 5 4 6 2
Output:
4
Explain test examples: Longest subsequence Series A[1] = 1 < The[2] = 2 < The[4] = 4 < The[5] = 6, This is the sequence length 4.

First use 1 b array to mark the sequence length increased at position b[in] any. A variable max marking the longest sequence length increases. Initially b[in] = 1 for all i, max = 1.
Browse row a with 1 loop.
While browsing, in each element row we started browsing backwards. If B[in] <= b[j] and a[in] > the[j] and b[in] <= Max, we increased b[in] up = b[j] + 1; Check and assign the max.
CEO:
Initially
the[] : 1 2 5 4 6 2
b[] : 1 1 1 1 1 1

Browse Times 1: (i=1)
the[1] = 2. Browse from a[0] about a[0].
we see b[1] <= b[0] and a[1] > the[0] and b[1] <=max => b[1] = b[0] + 1 = 2, max = b[1] = 2.
Browse Times 2: (i=2)
the[2] = 5. Browse from a[1] about a[0].
+/ At a[1], we see b[2] <= b[1] and a[2] > the[1] and b[2] <=max => b[2] = b[1] + 1 = 3, max = b[2] = 3.
+/ At a[0], b[2] =3 > b[0] => do nothing.
Browse Times 3: (i=3)
the[3] = 4. Browse from a[2] about a[0].
+/ At a[2], we see b[3] <= b[2] but a[3] < the[2] => do nothing
+/ At a[1], we see b[3] <= b[1] and a[3] > the[1] and b[3] <=max => b[3] = b[1] + 1 = 3, max unchanged.
+/ At a[0], b[3] =3 > b[0] => do nothing.
Browse Times 4: (i=4)
the[4] = 6. Browse from a[3] about a[0].
+/ At a[3], we see b[4] <= b[3] and a[4] > the[3] and b[4] <=max => b[4] = b[3] + 1 = 4, max = b[4] = 4.
+/ At a[2], we see b[4] = 4 > b[2] => do nothing.
+/ At a[1], we see b[4] = 4 > b[1] => do nothing.
+/ At a[0], we see b[4] = 4 > b[0] => do nothing.
Browse Times 5: (i=5)
the[5] = 2. Browse from a[4] about a[0].
+/ At a[4], we see b[5] <= b[4] but a[5] < the[4] => do nothing
+/ the[3], the[2], the[1] same
+/ At a[0], we see b[5] <= b[0] and a[5] > the[0] and b[5] <=max => b[5] = b[0] + 1 = 2, max unchanged.
So we have:
the[] : 1 2 5 4 6 2
b[] : 1 2 3 3 4 2
max = 4.