码迷,mamicode.com
首页 > 编程语言 > 详细

LIS的树状数组实现

时间:2015-10-21 20:50:09      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

  

LIS的树状数组实现

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4898994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!