LIS的树状数组实现

时间:2022-10-23 19:27:29
#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;
}