poj 3903 poj 2533 (LIS模板题)

pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik.

Sample Input

6
5 2 1 4 5 3
3
1 1 1
4
4 3 2 1
Sample Output

3
1
1

 

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8 
 9 int a[100010] ;
10 int f[100010] ;
11 //int dp[100010] ;
12 int n ;
13 
14 int bsearch(int size, const int &a) {
15     int l=0, r=size-1;
16     while( l <= r ){
17         int mid = (l+r)/2;
18         if( a > f[mid-1] && a <= f[mid] ) return mid;// >&&<= 换为: >= && <
19         else if( a < f[mid] ) r = mid-1;
20         else l = mid+1;
21     }
22 }
23 
24 int LIS()
25 {
26     int i, j, size = 1;
27     f[0] = a[0];
28     //dp[0] = 1;
29     for( i=1; i < n; ++i )
30     {
31         if( a[i] <= f[0] ) j = 0; // <= 换为: <
32         else if( a[i] > f[size-1] ) j = size++;// > 换为: >=
33         else j = bsearch(size, a[i]);
34         f[j] = a[i];
35         //dp[i] = j+1;
36     }
37     return size;
38 }
39 
40 
41 int main ()
42 {
43   //  freopen("in.txt","r",stdin) ;
44     while(scanf("%d" , &n) !=EOF)
45     {
46         int i ;
47         for (i = 0; i < n ; i++)
48             scanf("%d" , &a[i]) ;
49         printf("%d\n" , LIS()) ; // 求最大递增/上升子序列(如果为最大非降子序列,只需把上面的注释部分给与替换)
50     }
51 
52 
53     return 0 ;
54 }
View Code

 

posted @ 2015-05-31 20:58  __Meng  阅读(185)  评论(0编辑  收藏  举报