最终的 len 就是 最长上升子序列的长度 ,枚举 O(n) ,二分 O(logn) ,总的时间复杂度 O(nlogn)
代码
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>usingnamespace std;constint N =1e5+10;int n;int a[N];int f[N];intmain(){
cin >> n;for(int i =1; i <= n; i++) cin >> a[i];int len =0;for(int i =1; i <= n; i++){int l =0, r = len;while(l < r){int mid =(l + r +1)>>1;if(f[mid]< a[i]) l = mid;else r = mid -1;}
len =max(len, r +1);
f[r +1]= a[i];}
cout<<len<<endl;return0;}