HDU 2372 ||SDUT 2384 El Dorado(动态规划)

El Dorado

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

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 ai (-10000 ≤ ai ≤ 10000 ), where aiis the ith 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

Source

2008水题公开赛(比速度,OJ压力测试)

Recommend

lcy

 解题报告:这道题是一道动态规划题,题意就是求递增长度为m的总的个数,状态方程为dp[i][j] += dp[k][j - 1];其中dp[i][j]的含义:到第i个数时递增长度为j的个数;

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 110;
__int64 dp[N][N], ans;//dp[i][j]表示到第i个数的时候递增个数为j的个数
int m, n, a[N];
void DP()
{
int i, j, k;
memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; ++i)
{
dp[i][1] = 1;
}
for (j = 2; j <= m; ++j)//找长度为j的增长序列
{
for (i = j; i <= n; ++i)//遍历一次
{
for (k = j -1; k < i; ++k)//比较时应该是后面的和前面的作比较k<i作为条件
{
if (a[i] > a[k])
{
dp[i][j] += dp[k][j - 1];//状态方程dp[i][j]表示第i数时,递增长度为j的个数
}
}
}
}
}
int main()
{
int i;
while (scanf("%d%d", &n, &m) != EOF && n && m)
{
memset(a, 0, sizeof(a));
for (i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
}
DP();
ans = 0;
for(i = m; i <= n; ++i)
{
ans += dp[i][m];//把递增长度为m的序列数全加起来
}
printf("%I64d\n", ans);
}
return 0;
}



 

转载于:https://www.cnblogs.com/lidaojian/archive/2012/03/05/2380894.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值