El Dorado 2010.3.6
DP:求一个序列中长度为k的上升序列有多少个。
注意,枚举长度的时候要放在最外层循环。
El Dorado
Time Limit:1000MS Memory Limit:65536K
Total Submit:29 Accepted:9
Description
Bruce Force has gone to Las Vegas, the El Doradofor gamblers. He is interested especially in one betting game, where a machineforms a sequence of n numbers by drawing random numbers. Each player shouldestimate beforehand, how many increasing subsequences of length k will exist inthe sequence of numbers.
A subsequence of a sequence a1, ..., an isdefined as ai1, ..., ail, where 1 ≤ i1 < i2 < ...< il ≤ n. The subsequence is increasing, if aij-1 < aij for all 1 <j ≤ l.
Bruce doesn't trust the Casino to count thenumber of increasing subsequences of length k correctly. He has asked you ifyou can solve this problem for him.
Input
The input contains several test cases. Thefirst line of each test case contains two numbers n and k (1 ≤ k ≤ n ≤ 100), wheren is the length of the sequence drawn by the machine, and k is the desiredlength of the increasing subsequences. The following line contains n pairwisedistinct integers ai (-10000 ≤ ai ≤ 10000 ), where ai is the ith number in the sequence drawn by themachine.
The last test case is followed by a linecontaining two zeros.
Output
For each test case, print one line with thenumber 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 fitsinto a 64 bit signed integer (in C/C++, you may use the data type "longlong", in Java the data type "long").
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
ULM 2008
#include <cstdio>
#include <cstring>
#define MAXN 100+10
__int64 f[MAXN][MAXN];
__int64 num[MAXN];
int n,k,i,j,p;
__int64 goal;
int main()
{
while (scanf("%d %d",&n,&k),n)
{
memset(f,0,sizeof(f));
memset(num,0,sizeof(num));
goal=0;
for(i=1;i<=n;i++)
scanf("%I64d",&num[i]);
for(i=1;i<=n;i++)
f[i][1]=1;
for(j=2;j<=k;j++)
for(i=1;i<=n;i++)
for(p=1;p<i;p++)
if(num[p]<num[i])
f[i][j]+=f[p][j-1];
for(i=1;i<=n;i++)
goal+=f[i][k];
printf("%I64d\n",goal);
}
return 0;
}