hdu 4991 Ordered Subsequence(Bestcoder Round #8 1003)

来源:互联网 发布:美工需要会什么软件 编辑:程序博客网 时间:2024/05/22 17:08

Ordered Subsequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 214    Accepted Submission(s): 109


Problem Description
A numeric sequence of ai is ordered if a1<a2<……<aN. Let the subsequence of the given numeric sequence (a1, a2,……, aN) be any sequence (ai1, ai2,……, aiK), where 1<=i1<i2 <……<iK<=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 21 1 27 31 7 3 5 9 4 8
 

Sample Output
212
 


  数据的离散化+树状数组

 

  第一次了解离散化点击打开链接


//562ms#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn=10000+100;const int mod=123456789;int a[maxn+10],b[maxn+10];int dp[110][maxn+10];int low(int k){    return (k&-k);}int getsum(int k,int u){    int ans=0;    while(k>0)    {        ans=(ans+dp[u][k])%mod;        k-=low(k);    }    return ans;}void update(int u,int k,int v){    while(k<maxn)    {        dp[u][k]=(dp[u][k]+v)%mod;        k+=low(k);    }}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++)        {           scanf("%d",&a[i]);           b[i]=a[i];        }        if(n<m)        {            printf("0\n");            continue;        }        sort(b+1,b+n+1);//数据的离散化        int size=unique(b+1,b+n+1)-b-1;//离散后的长度,去重        for(int i=1;i<=n;i++)        a[i]=lower_bound(b+1,b+n+1,a[i])-b;//离散后的值        update(1,a[1],1);       // printf("jsd\n");        for(int i=2;i<=n;i++)        {            for(int j=m;j>=2;j--)//枚举前面所有可能的长度            {                int sum=getsum(a[i]-1,j-1);                update(j,a[i],sum);            }            update(1,a[i],1);        }        int ans=getsum(size,m);        printf("%d\n",ans);    }    return 0;}


0 0