第五周第二次训练 D题 树状数组

Description

Given a sequence of N (1 ≤ N ≤ 10,000) integers S1, ..., SN (0 ≤ Si < 100,000), compute the number of increasing subsequences of S with length K (1 ≤ K ≤ 50 and K ≤ N); that is, the number of K-tuples i1, ..., iK such that 1 ≤ i1 < ... < iK ≤ N and Si1 < ... < SiK.

Input

The first line contains the two integers N and K. The following N lines contain the integers of the sequence in order.

Output

Print a single integer representing the number of increasing subsequences of S of length K, modulo 5,000,000.

本题求的是k长的递增序列。方法是利用树状数组来求。


#include<algorithm>
using namespace std;
#define maxn 10010
#define mod 5000000
int b[maxn],c[maxn][51],ans[maxn],a[maxn],n,m;
int id[100010];
int lowbit(int i)
{return i&(-i);}
int sum(int k,int i)
{
	int s=0;
	while(i)
	{
		s+=c[i][k];
		s%=mod;
		i-=lowbit(i);
	}
	return s;
}
void update(int k,int i,int val)
{
	while(i<=maxn)
	{
		c[i][k]+=val;
		c[i][k]%=mod;
		i+=lowbit(i);
	}
}
int main()  
{  
	while(~scanf("%d%d",&n,&m))
	{
		int i,j,t;
		for(i=0;i<n;i++)
		{
			scanf("%d",&b[i]);
			a[i]=b[i];
		}
		sort(a,a+n);
		t=1;
		id[a[0]]=t++;
		for(i=1;i<n;i++)
			if(a[i]!=a[i+1])
				id[a[i]]=t++;
		for(i=0;i<=n;i++)
			ans[i]=1;
		memset(c,0,sizeof(c));
		for(i=1;i<=m;i++)
			for(j=0;j<n;j++)
			{	
				update(i,id[b[j]],ans[j]);		
				ans[j]=sum(i,id[b[j]]-1);							
			}
		printf("%d\n",sum(m,n));//不是sum(m,id(n-1));
	}	
    return 0;  
} 

 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值