#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;
}
牛客OI周赛15-普及组(多元组,树状数组优化dp,求固定长度的上升子序列的个数)
最新推荐文章于 2022-04-21 16:56:36 发布