【树状数组 + 离散化 + DP】 HDU 5542

16 篇文章 0 订阅
8 篇文章 0 订阅

The Battle of Chibi

Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2035    Accepted Submission(s): 714


Problem Description
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army. But all generals and soldiers of Cao Cao were loyal, it's impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out  N  information to be leaked, in happening order. Each of the information was estimated to has  ai  value in Cao Cao's opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact  M  information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the  N  information and just select  M  of them. Find out how many ways Gai Huang could do this.
 

Input
The first line of the input gives the number of test cases,  T(1100) T  test cases follow.

Each test case begins with two numbers  N(1N103)  and  M(1MN) , indicating the number of information and number of information Gai Huang will select. Then  N  numbers in a line, the  ith  number  ai(1ai109)  indicates the value in Cao Cao's opinion of the  ith  information in happening order.
 

Output
For each test case, output one line containing  Case #x: y, where  x  is the test case number (starting from 1) and  y  is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by  1000000007(109+7) .
 

Sample Input
  
  
2 3 2 1 2 3 3 2 3 2 1
 

Sample Output
  
  
Case #1: 3 Case #2: 0
Hint
In the first cases, Gai Huang need to leak 2 information out of 3. He could leak any 2 information as all the information value are in increasing order. In the second cases, Gai Huang has no choice as selecting any 2 information is not in increasing order.
 

Source
 
题意:给出两个数 n 和 m,表示接下来有 n 个数的数列,要求在这个数列里找出长度为 m 的严格上升子序列,问有多少种方案

比如样例的 3 2  1 2 3 就是 (1,2) (1,3) (2,3)  三种

而另一个例子  4 2   1 1 3 5 就是 (1,3)(1,5)(1,3)(1,5)(3,5)五种,不需要组合出的值不同,只要方案不同就ojbk

思路:都知道最长上升子序列是DP的作法,那这个题目自然也想到 DP

DP[i][j]  表示长度为 i ,以 num[i]为结尾元素的子序列的个数

就比如 4 2  1 2 3 4    那么 DP[2][3]就表示 长度为 2 的以 num[3] 作为结尾元素的子序列个数,分别是 (1,3) (2,3)

那么 DP[i][j]如何通过前面的状态转移得到呢? 我们要拿第 j 个元素作为子序列结尾的时候,那么这个子序列前面固然还有 (i - 1) 个元素,也就是说我们要找到 以 第 j 个元素之前的元素作为结尾的长度为 i - 1 的子序列方案数

当 i = 1 的时候,每个数作为结尾的方案数都是 1,所以先把 i = 1初始化为 1

然后从 i = 2开始,第 j 个数去获取 它前面的数长度为 (i - 1) 的子序列数

如果直接暴力去循环 1 到 j - 1 找到前面每个数的(i - 1)长度方案,时间复杂度为 O(n),外层的两层DP时间复杂度为O(n^2) ,所以总时间复杂度为 O(n^3) 虽然n最大是 1000,但是 n^3 = 10^9,四秒肯定超时

但是外层的两层是必要的,没法做优化,那么只能在最后面的找(i - 1)长度方案做优化,首先想,我们是从左到右一次找的,而且当前的方案数与前面的有关系(要找到前面所有数的(i-1)长度方案),与后面的是没有关系的,所以他是一个线性的关系,既然是这样,那么我们就可以用一个树状数组来保存前面的方案数,然后每找到一个 j 的方案就把它 add 到树状数组里,然后后面的就可以从树状数组里 getSum 了

现在作法已经清楚,但是还有一个问题,题目虽然给的 n 很小,但是里面的值可以很大,这样就没法用树状数组做了,因为开不了那么大,所以先对这个序列做一次离散化,把它打成 1000以内的数,就可以用树状数组维护了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
#define ll long long  
#define mem(a,x) memset(a,x,sizeof(a))  
#define lowbit(x) (x & (-x))  
#define maxn 1005
#define mod 1000000007

int a[maxn],b[maxn],tree[maxn];
int cnt,ans,n,m;
ll dp[maxn][maxn];

ll getSum(int x){
	ll tmp = 0;
	while(x > 0){
		tmp += tree[x];
		tmp %= mod;
		x -= lowbit(x);
	}
	return tmp;
}
void add(int x,int y,ll val){
	while(x <= y){
		tree[x] += val;
		tree[x] %= mod;
		x += lowbit(x);
	}
}
void discre(){
	map<int,int>ma;
	sort(b + 1,b + 1 + n);
	cnt = 1;
	for(int i = 1;i <= n;i++){
		if(i == 1){
			ma[b[i]] = cnt;
		}else{
			if(b[i] != b[i - 1]){
				ma[b[i]] = ++cnt;
			}else{
				ma[b[i]] = cnt;
			}
		}
	}
	for(int i = 1;i <= n;i++){
		a[i] = ma[a[i]];
	}
}
int main(){
	int t,Case = 1;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d",&n,&m);
		for(int i = 1;i <= n;i++){
			scanf("%d",&a[i]);
			b[i] = a[i];
		}
		mem(dp,0);
		for(int i = 0;i <= 1000;i++){
			dp[1][i] = 1;
		}
		discre(); // 离散化 
		for(int i = 2;i <= m;i++){
			mem(tree,0);
			for(int j = i - 1;j <= n;j++){
				ll tmp = getSum(a[j] - 1);
				dp[i][j] = tmp;
				add(a[j],cnt,dp[i - 1][j]);
			}
		}
		ans = 0;
		for(int i = 1;i <= n;i++){
			ans = (ans + dp[m][i]) % mod;
		}
		printf("Case #%d: %lld\n",Case++,ans);
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值