百练2757 最长上升子序列的长度 树状数组

啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊,

之前觉得思路简单,代码复杂看不懂,,现在是代码简单,思路复杂,还是看不懂

啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

我怎么这么懒,懒得连注释都懒得写,,这以后可得怎么办啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

然后这题我们用树状数组做

 

首先找s1数组中的元素在排序后的数组中的位置,也是第几大,

然后查询比他小的最长上升子序列的长度,然后+1,判断最大值

然后再把他更新进去。。。。。。。。。。

#include<iostream>
#include<algorithm>
#include<fstream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<limits.h>
#include<queue>
#include<stack>
using namespace std;

const int maxn=1e5+10;

int p[maxn],s1[maxn],s2[maxn],n;

int lowbit(int x)
{
    return x&(-x);

}

void change(int x,int y)
{
    while(x<=n)
    {
        p[x]=max(p[x],y);
        x+=lowbit(x);
    }
}

int getsum(int x)
{
    int ans=0;
    while(x)
    {
        ans=max(ans,p[x]);
        x-=lowbit(x);
    }
    return ans;
}


int main()
{
    int len,i,ans,sum,a;
    scanf("%d",&n);
    for(i=0; i<n; ++i)
    {
        scanf("%d",&s1[i]);
        s2[i]=s1[i];
    }
    sort(s2,s2+n);
    len=unique(s2,s2+n)-s2;
    for(i=0,ans=0; i<n; ++i)
    {
        a=lower_bound(s2,s2+len,s1[i])-s2+1;
        sum=getsum(a-1)+1;
        ans=max(ans,sum);
        change(a,sum);
    }
    cout<<ans<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值