LIS的树状数组实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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 @   Painting、时光  阅读(478)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示