最长上升子序列:是严格上升的,a<b<c<d这样的,即为严格单调递增,用lower_bound
lower_bound:返回序列中大于等于key值的第一个数
比如说序列 1 2 3 4 5,key值为3,使用lower_bound返回第三个数
最长不下降子序列:不是严格上升,可以存在多个数相等的情况,用upper_bound
upper_bound:返回序列中严格大于key值的第一个数
比如说序列1 2 3 4 5,key值为3,使用upper_bound返回第四个数
Hdu 1950 求最长上升子序列
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algorithm> 11 using namespace std; 12 13 typedef long long LL; 14 const int INF = (1<<30)-1; 15 const int mod=1000000007; 16 const int maxn=1000005; 17 18 int a[maxn],f[maxn]; 19 int n; 20 21 int main(){ 22 int T; 23 scanf("%d",&T); 24 while(T--){ 25 scanf("%d",&n); 26 for(int i=1;i<=n;i++) scanf("%d",&a[i]); 27 28 int len=1; 29 f[1]=a[1]; 30 for(int i=2;i<=n;i++){ 31 if(a[i]>f[len]) f[++len]=a[i]; 32 else{ 33 int pos=lower_bound(f+1,f+len+1,a[i])-f;//lower_bound返回序列中大于等于key值的第一个数 34 f[pos]=a[i]; 35 } 36 } 37 printf("%d\n",len); 38 } 39 return 0; 40 }