牛客OI周赛15-普及组(多元组,树状数组优化dp,求固定长度的上升子序列的个数)

多元组:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn=2e5+10;
const LL mod=1e9+7;
LL dp[55][maxn];//dp[i][j]表示长度位i的以第j个元素结尾的上升子序列的个数
LL a[maxn];
LL c[maxn];
vector<LL> v;
LL n,m;
LL mx;
LL lowbits(LL x)
{
    return x&(-x);
}
LL sum(int x)
{
    LL res=0;
    while(x>0){
        res+=c[x];
        res%=mod;
        x-=lowbits(x);
    }
    return res%mod;
}
void add(LL x,LL d)
{
    while(x<=mx){
        c[x]+=d;
        c[x]%=mod;
        x+=lowbits(x);
    }
}
LL getid(LL x)
{
   return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(LL i=1;i<=n;i++){
        scanf("%lld",&a[i]);v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    mx=v.size();
    for(LL i=1;i<=n;i++){
        dp[1][i]=1;
        a[i]=getid(a[i]);
    }
    for(LL i=2;i<=m;i++){//在这里我们对于长度相同的子序列进行一次求解,
        memset(c,0,sizeof(c));//求长度为i的上升子序列的个数时,我们先初始化c数组(树状数组)
        for(LL j=1;j<=n;j++){
            dp[i][j]=sum(a[j]-1);//因为前面长度为i-1的以a[j]-1为结尾的上升子序列的个数已经求出来了,所以我们直接区间求和
            add(a[j],dp[i-1][j]);//用当前的以第j个元素结尾长度为i-1的子序列的个数去单点更新整个c数组(有点类似于二维偏序)
        }
    }
    LL ans=0;
    for(LL i=1;i<=n;i++){
        ans+=dp[m][i];
        ans%=mod;
    }
    printf("%lld\n",ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值