【Codeforces Testing Round 12C】【DP 树状数组优化】Subsequences n个不同数,长度为m的LIS数

C. Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.

Input

First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.

Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.

Output

Print one integer — the answer to the problem.

Sample test(s)
input
5 2
1
2
3
5
4
output
7

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n,m,x;
struct BIT
{
	LL f[N];
	void add(int x,LL v)
	{
		for(;x<=n;x+=x&-x)f[x]+=v;
	}
	LL cnt(int x)
	{
		LL tmp=0;
		for(;x;x-=x&-x)tmp+=f[x];
		return tmp;
	}
}bit[12];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		++m;
		MS(bit,0);
		LL ans=0;
		for(int i=1;i<=n;++i)
		{
			scanf("%d",&x);
			bit[1].add(x,1);
			if(m==1)++ans;
			for(int j=2;j<=m;++j)
			{
				LL tmp=bit[j-1].cnt(x-1);
				bit[j].add(x,tmp);
				if(m==j)ans+=tmp;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*
【题意】
给你n(1e5)个数a[],每个数都在[1,n]范围且数值各不相同。
我们想要求,长度为m+1(0<=m<10)的LIS的数量是多少个。

【类型】
DP 树状数组优化

【分析】
这道题很好找到突破口,就是m很小。
于是,我们可以设想一个DP。
用f[i][j]表示最后一个数的位置为i,长度为j的LIS数。
初始化f[1~n][1]=1,答案是∑f[1~n][m]。
具体的转移方程则是——f[i][j]=∑f[p][j-1],p<i且a[p]<a[i]。
然而这个DP方程却是O(n*n*m)的。
于是,我们要采取优化策略。我们需要找到在它之间比它小的数。
在它之间,可以通过扫描顺序解决。
而比它小,则可以通过树状数组动态更新和解决。

【时间复杂度&&优化】
O(nmlog(n))
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值