hdoj 2372 El Dorado

题号:hdoj 2372;链接:http://acm.hdu.edu.cn/showproblem.php?pid=2372

题目:

El Dorado

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 418    Accepted Submission(s): 197


Problem Description
Bruce Force has gone to Las Vegas, the El Dorado for gamblers. He is interested especially in one betting game, where a machine forms a sequence of n numbers by drawing random numbers. Each player should estimate beforehand, how many increasing subsequences of length k will exist in the sequence of numbers. 



Bruce doesn't trust the Casino to count the number of increasing subsequences of length k correctly. He has asked you if you can solve this problem for him. 

 

Input
The input contains several test cases. The first line of each test case contains two numbers n and k (1 ≤ k ≤ n ≤ 100), where n is the length of the sequence drawn by the machine, and k is the desired length of the increasing subsequences. The following line contains n pairwise distinct integers a i (-10000 ≤ a i ≤ 10000 ), where a iis the i th number in the sequence drawn by the machine. 

The last test case is followed by a line containing two zeros. 
 

Output
For each test case, print one line with the number of increasing subsequences of length k that the input sequence contains. You may assume that the inputs are chosen in such a way that this number fits into a 64 bit signed integer . 
 

Sample Input
  
  
10 5 1 2 3 4 5 6 7 8 9 10 3 2 3 2 1 0 0
 

Sample Output
  
  
252 0
 

解题思路:

题目要求一个序列中长度为k的递增子序列的个数,我们可以用dp来做。dp[i][j]的含义:i表示子序列中尾元素的下标,j表示子序列的长度。这样的话,我们可以得到状态转移方程:dp[i][j] = sum(dp[k][j-1]) 其中(a[k]< a[i])(k>=j-1&&k<i),最后,我们所要求的结果就是sum(dp[i][k])(i=1,2,...,n).


代码:

#include <iostream>
#include <cstring>
using namespace std;

const int MAXN = 110;
__int64 dp[MAXN][MAXN];
int a[MAXN];

int main()
{
	std::ios::sync_with_stdio(false);
	
	int n, k;
	
	while(!cin.eof())
	{
		long long ans = 0;
		cin >> n >> k;
		
		if(0 == n && 0 == k) break;
		
		for(int i = 1; i <= n; i++)
			cin >> a[i];	
		
		memset(dp, 0, sizeof(dp));
		for(int i = 1; i <= n; i++)
			dp[i][1] = 1;
			
		for(int j = 2; j <= k; j++)
		{
			for(int i = j; i <= n; i++)
			{
				for(int m = j - 1; m < i; m++)
				{
					if(a[i] > a[m])
					{
						dp[i][j] += dp[m][j-1];
					}
				}
			}
		}
		
		for(int i = 1; i <= n; i++)
			ans += dp[i][k];
		
		cout << ans << endl;
	}
	
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值