HDU 4991 Rabbit Kingdom(二维树状数组)

来源:互联网 发布:centos7 php配置文件 编辑:程序博客网 时间:2024/06/04 20:09
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
 

其实题意很好理解。设dp[i][j]表示以第i个结尾并且有j个的数量。
则dp[i][j]=sum(dp[k][j-1]);接下来就是树状数组维护了。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int MOD  = 123456789;const int maxn=10000+5;int n,m,len;//dp[i][j]=sum(dp[k][j-1])(k<i);LL c[105][maxn];//i第几层的树状数组LL a[maxn],b[maxn];int lowbit(int x){    return x&(-x);}void update(int x,LL val,int f){    while(x<=len)    {        c[f][x]=(c[f][x]+val)%MOD;        x+=lowbit(x);    }}LL query(int x,int f){    LL s=0;    while(x>0)    {        s=(s+c[f][x])%MOD;        x-=lowbit(x);    }    return s;}int main(){    while(~scanf("%d%d",&n,&m))    {        CLEAR(c,0);        REP(i,n)        {            scanf("%I64d",&a[i]);            b[i]=a[i];        }        sort(b,b+n);        len=unique(b,b+n)-b;        REP(i,n)        {            int pos=lower_bound(b,b+len,a[i])-b+1;            update(pos,1,1);            REPF(j,2,m)            {                LL num=query(pos-1,j-1);                update(pos,num,j);            }        }        printf("%I64d\n",query(len,m));    }    return 0;}


0 0