1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N = 50001; 6 int a[N]; 7 int b[N]; 8 int Search(int num, int low, int high){ 9 int mid; 10 while(low <= high){ 11 mid = (low + high)/2; 12 if(num >= b[mid]) low = mid + 1; 13 else high = mid - 1; 14 } 15 return low; 16 } 17 int DP(int n){ 18 int i, len, pos; 19 b[1] = a[1]; 20 len = 1; 21 for(i = 2; i <= n; i++){ 22 if(a[i] >= b[len]) 23 b[++len] = a[i]; 24 else{ 25 pos = Search(a[i],1,len); 26 //在b[]数组中找出第一个比a[i]大的位置并且让a[i]替代这个位置 27 b[pos] = a[i]; 28 } 29 } 30 return len; 31 } 32 int main(){ 33 int n, i, j; 34 scanf("%d", &n); 35 for(i = 1; i <= n; ++i) 36 scanf("%d", &a[i]); 37 int len = DP(n); 38 printf("%d ", len); 39 return 0; 40 }