HDU 5542 The Battle of Chibi(dp+树状数组)

题意:给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列。

分析:《算法竞赛进阶指南》P312-313。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1006, INF = 0x3f3f3f3f, P = 1000000007;
int n, m, a[N], b[N], c[N], f[N][N], num;

void add(int x, int y) {
	while (x <= n + 1) {
		c[x] = (c[x] + y) % P;
		x += x & -x;
	}
}

int ask(int x) {
	int ans = 0;
	while (x) {
		ans = (ans + c[x]) % P;
		x -= x & -x;
	}
	return ans;
}

void The_Battle_of_Chibi() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	memcpy(b, a, sizeof(b));
	sort(b + 1, b + n + 1);
	for (int i = 1; i <= n; i++)
		a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b + 1;
	a[0] = f[0][0] = 1;
	for (int i = 1; i <= m; i++) {
		memset(c, 0, sizeof(c));
		add(1, f[i-1][0]);
		for (int j = 1; j <= n; j++) {
			f[i][j] = ask(a[j] - 1);
			add(a[j], f[i-1][j]);
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) ans = (ans + f[m][i]) % P;
	printf("Case #%d: %d\n", ++num, ans);
}

int main() {
	int t;
	cin >> t;
	while (t--) The_Battle_of_Chibi();
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值