Ordered Subsequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 192 Accepted Submission(s): 98
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.
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
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
//hdu 4991
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
using namespace std;
typedef long long LL;
const int MAX=0xfffffff;
int n,m;
const int mx=10100;
const int mod=123456789;
int a[mx],dp[mx][110],b[mx],c[mx],R[mx];
map<int,int>hash; //hash,如果开成数组肯定会太大
void update(int x,int val)
{
while(x<=n)
{
c[x]=(c[x]+val)%mod;
x+=(x&(-x));
}
return;
}
int query(int x)
{
int ans=0;
while(x>0)
{
ans=(ans+c[x])%mod;
x-=(x&(-x));
}
return ans;
}
int main( )
{
//freopen("1.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",a+i);
memcpy(b,a,sizeof(a));
sort(a+1,a+n+1);
int N=0;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
dp[i][1]=1;
if(a[i]==a[i-1]&&i>1) continue;
hash[a[i]]=++N;
}
for(int i=1;i<=n;i++) R[i]=hash[b[i]];
for(int j=2;j<=m;j++) //每次循环一层
{
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
dp[i][j]=query(R[i]-1);
update(R[i],dp[i][j-1]);
}
}
int ans=0;
for(int i=1;i<=n;i++)
ans=(ans+dp[i][m])%mod;
printf("%d\n",ans);
}
return 0;
}