最長上升子序列
阿新 • • 發佈:2020-07-26
描述
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, the sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e.g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences of this sequence are of length 4, e.g., (1, 3, 5, 8).
給出一個長度為N的數字串,找出一個嚴格上升的數字序列來.
輸入
The first line of input contains the length of sequence N (1 <= N <= 1000). The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces.
輸出
Output must contain a single integer - the length of the longest ordered subsequence of the given sequence.
樣例
輸入
7 1 7 3 5 9 4 8
輸出
4
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,a[10001],sum,ans=INT_MIN; 4 int f[1001]; 5 int main() { 6 scanf("%d",&n);7 for(int i=1; i<=n; i++) 8 cin>>a[i]; 9 for(int i=1; i<=n; i++) { 10 int sum=0; 11 for(int j=1; j<=i; j++) { 12 if(a[i]>a[j]) 13 sum=max(sum,f[j]); 14 f[i]=sum+1; 15 } 16 } 17 for(int i=1;i<=n;i++) 18 ans=max(ans,f[i]); 19 printf("%d",ans); 20 return 0; 21 }