标签:style blog io color for sp 数据 div log
给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度。
如:1 9 10 5 11 2 13的最长单调递增子序列是1 9 10 11 13,长度为5。
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 int riseSubString(int a[], int n) 5 { 6 int i,l,r,m,s[100000],top = 0; 7 if (n == 0) 8 return 0; 9 s[0] = a[0]; 10 for (i=1; i < n; i++) 11 { 12 if (s[top] < a[i]) 13 s[++top] = a[i]; 14 else 15 { 16 l = 0; 17 r = top; 18 m = top / 2; 19 while ((l + 1) < r) //二分,l与r间隔1时结束循环,再判断哪个是大于a[i]最小值 20 { 21 if (a[i] <= s[m]) 22 { 23 r = m; 24 m = (r+l)/2; 25 } 26 else 27 { 28 l = m; 29 m = (l+r)/2; 30 } 31 } 32 if (s[l] > a[i]) 33 s[l] = a[i]; 34 else 35 s[r] = a[i]; 36 } 37 } 38 return top + 1; 39 } 40 int main() 41 { 42 int a[100001]; 43 int n,i; 44 while (scanf("%d",&n) != -1){ 45 for (i=0; i < n; i++) 46 scanf("%d",&a[i]); 47 printf("%d\n",riseSubString(a,n)); 48 } 49 return 0; 50 }
标签:style blog io color for sp 数据 div log
原文地址:http://www.cnblogs.com/george-cw/p/4067617.html