CodeForces 597C_Subsequences

Description

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.


大意是从n个数中找有多少个长度为k+1的升序子串。

dp[k][m]代表以m结尾的长度为k的子串数目,每次更新的时候都需要把dp[k-1][1]~dp[k-1][a[i]]的所有值加起来,直接加绝对会tle

一开始也不会做,后来问了同学之后才知道有树状数组,推荐: http://dongxicheng.org/structure/binary_indexed_tree/

#include<iostream>
using namespace std;
typedef unsigned long long ull;
int n;
int k;
const int MAXN = 1e5 + 10;
const int MAXK = 15;
int a[MAXN];
ull dp[MAXK][MAXN];//以n为结尾长度为k的数量
//树状数组------------------------
int lowbit(int t){
	return t&(t ^ (t - 1));
}
ull sum(int k,int end){
	ull sum = 0;
	while (end > 0){
		sum += dp[k][end];
		end -= lowbit(end);
	}
	return sum;
}
void pluss(int k, int pos, ull num){
	while (pos <= n){
		dp[k][pos] += num;
		pos += lowbit(pos);
	}
}
//--------------------------------
int main()
{
	while (cin >> n >> k){
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i){
			cin >> a[i];
			for (int tk = k + 1; tk >= 2; --tk){
				pluss(tk, a[i], sum(tk - 1, a[i]));
			}
			pluss(1, a[i], 1);
		}
		ull ans = 0;
		ans = sum(k + 1, n);
		cout << ans << endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值