南阳214----单调递增子序列(二)
1 /* 2 用一个数组记录递增子序列,保持s处为最长子序列的最后一个值 3 当输入x小于d[s]时,向前找x的位置覆盖即可 4 复杂度与经典算法同为n*n 5 加入二分查找,优化后为n*logn 6 */ 7 #include<cstdio> 8 #define inf 1<<30 9 int d[100005],s; 10 11 void solven(int x) 12 { 13 int t,left,right,mid; 14 if(s == 0 || d[s] < x) 15 d[++s] = x; 16 else 17 { 18 left = 1; 19 right = s; 20 while(left <= right) 21 { 22 mid = (left + right)/2; 23 if(d[mid]>=x && d[mid-1]<x) 24 { 25 d[mid] = x; 26 return ; 27 } 28 if(d[mid] > x) 29 right = mid - 1; 30 else 31 left = mid + 1; 32 } 33 } 34 } 35 36 int main() 37 { 38 int n,x,i,t; 39 while(~scanf("%d",&n)) 40 { 41 s = 0; 42 d[0] = -inf; 43 while(n--) 44 { 45 scanf("%d",&x); 46 solven(x); 47 } 48 printf("%d\n",s); 49 } 50 return 0; 51 }