Description
A Numeric sequence of
AIis ordered if
A1<
A2< ... <
an. Let the subsequence of the given numeric sequence (
A1,
A2, ...,
an) is any sequence (
Ai1,
AI2, ...,
AiK), where 1 <=
I1<
I2< ... <
IK<=
N. For example, sequence (1, 7, 3, 5, 9, 4, 8) have ordered Subsequences, E. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences is of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence-n integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer-the length of the longest ordered subsequence of the given sequence.
Sample Input
71 7 3 5 9 4 8
Sample Output
4
Note: The general memset array is assigned 0 or-1, and the other is to be implemented with loops
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;intMain () {intn,i,j,a[1005],d[1005]; while(SCANF ("%d", &n)! =EOF) { intsum=0; for(i=0; i<=n; i++) {D[i]=1; } for(i=1; i<=n; i++) {scanf ("%d",&A[i]); for(j=1; j<i; J + +) { if(a[j]<A[i]) d[i]=max (d[i],d[j]+1);//iterate over its previous element to find the maximum length of the tag in each element} sum=Max (sum,d[i]); } printf ("%d\n", sum); } return 0;}
Poj-2533-longest Ordered subsequence (lis template)