LIS的树状数组实现

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int C[1100];
const int MAXN = 10;
void update(int x, int ans)
{
    C[x] = ans ;
    while(x < MAXN){
        C[x] = max(C[x] , ans) ;
        x += x&-x;
    }
}

int query(int x)
{
    int ret = 0;
    while(x > 0){
        ret = max(ret,C[x]);
        x -= x&-x;
    }
    return ret;
}

int main()
{
    int n;
    int a[1100];
    int b[1100];
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        b[i] = a[i] ;
    }
    sort (b+1 , b+1+n) ;
    //int sz = unique(b+1 , b+1+n) - b; ;
    for (int i = 1 ; i <= n ; i ++){
        a[i] = lower_bound (b+1,b+1+n,a[i]) - b ;
    }
    for(int i = 1; i <= n; i++)
       printf("%d ", a[i]); 
    
    int ans = 0;
    for(int i = 1; i <= n; i++){
        ans = max(ans, query(a[i]-1)+1);
        if (ans > C[a[i]])
        update(a[i] , ans) ;
    }
    printf("%d\n", ans);
    return 0;
}

  

posted @ 2015-10-21 20:27  Painting、时光  阅读(478)  评论(0编辑  收藏  举报