Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 443 Accepted Submission(s): 198
Problem Description
A numeric sequence of a
i is ordered if a
1<a
2<……<a
N. Let the subsequence of the given numeric sequence (a
1, a
2,……, a
N) be any sequence (a
i1, a
i2,……, a
iK), where 1<=i
1<i
2 <……<i
K<=N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, eg. (1, 7), (3, 4, 8) and many others.
Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
Your program, when given the numeric sequence, must find the number of its ordered subsequence with exact m numbers.
Input
Multi test cases. Each case contain two lines. The first line contains two integers n and m, n is the length of the sequence and m represent the size of the subsequence you need to find. The second line contains the elements of sequence - n integers in the range from 0 to 987654321 each.
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
Process to the end of file.
[Technical Specification]
1<=n<=10000
1<=m<=100
Output
For each case, output answer % 123456789.
Sample Input
3 2 1 1 2 7 3 1 7 3 5 9 4 8
Sample Output
2 12
Source
/************
考虑dp[i][j]为以j结尾长度为i的递增子序列数目
则有dp[i][j]=sum(dp[i-1][k])满足num[k]<num[j]&&k<j;
因为求区间和可以用树状数组优化
*************/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<vector>
#define MOD 123456789
using namespace std;
const int maxn=10010;
int array[maxn];
int num[maxn],len;
int dp[110][maxn];
int low(int n){
return n&(-n);
}
void add(int *c,int pos,int v){
while(pos<=len){
c[pos]+=v;
c[pos]%=MOD;
pos+=low(pos);
}
}
int sum(int *c,int n){
int value=0;
while(n>0){
value+=c[n];
value%=MOD;
n-=low(n);
}
return value;
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=1;i<=n;++i){
scanf("%d",&num[i]);
array[i]=num[i];
}
sort(array+1,array+n+1);
len=unique(array+1,array+n+1)-array-1;
for(i=1;i<=n;++i){
num[i]=lower_bound(array+1,array+len+1,num[i])-array+1;
}
memset(dp,0,sizeof(dp));
len++;
add(dp[0],1,1);
for(i=1;i<=n;++i){
for(j=m;j>=1;--j){
add(dp[j],num[i],sum(dp[j-1],num[i]-1));
}
}
printf("%d\n",sum(dp[m],len));
}
return 0;
}