Rise longest sequence Ⅰ
Rise longest sequence Ⅱ
给定一个长度为N的数列,求数值严格单调递增的子序列的长度最长是多少。
输入格式
第一行包含整数N。
第二行包含N个整数,表示完整序列。
输出格式
输出一个整数,表示最大长度。
数据范围
1≤N≤1000,
−109≤数列中的数≤109
输入样例:
7
3 1 2 1 8 5 6
输出样例:
4
Solution as follows:
f
array is used to store the first i
end increases the maximum number of the sub-sequence number
f[i]
represents i
the number of the maximum number of sub-sequences at the end of rise
recursion formulas readily available:
f[i]=max(f[i],f[j]+1);
Which f[j]
represents f[i]
a number before, if f[j]<=f[i]
it has found a number can rise up to join this sequence being taken f[i]
with f[j]+1
the maximum value (so a new found +1) on it.
C ++ code:
#include<bits/stdc++.h>
using namespace std;
int a[1001],f[1001];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
{
f[i]=1;
for(int j=1;j<=i;j++)
if(a[j]<a[i])
f[i]=max(f[i],f[j]+1);
}
int ans=-1;
for(int i=1;i<=n;i++)
ans=max(ans,f[i]);
cout<<ans;
return 0;
}