最长增子序列 复杂度nlog(n) (By OWenT)
代码(C++)
1 //最长增子序列 复杂度nlog(n)
2 double list[10002],lengthRecord[10002];//元素记录,注意类型
3 int main()
4 {
5 int n;
6 int length = 1;
7 cin>>n;//数组个数
8 for(int i = 0 ; i < n ; i ++)
9 scanf("%lf",&list[i]);//注意类型
10 lengthRecord[0] = list[0];
11
12 for(int i = 1 ; i < n ; i ++)
13 {
14 //二分查找,复杂度 logn
15 int b,e,m;
16 b = 0;
17 e = length - 1;
18 while(b <= e && e >= 0)
19 {<br/>
20 m = (b + e) / 2;
21 if(lengthRecord[m] < list[i])
22 b = m + 1;
23 else<br/>
24 e = m - 1;
25 }<br/>
26 lengthRecord[b] = list[i];
27 if(b >= length)
28 length ++;
29 }
30 cout<<length;