codeforces 597C

枚举子序列的末尾,递推。

方案数:f[i = 以i结尾][k =子序列长度] = sum(f[j][k-1]),j < i。

转移就建立k个BIT。

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e5+5, K = 10;

ll C[K][N];

ll sum(ll C[],int x)
{
    ll re = 0;
    while(x > 0){
        re += C[x]; x &= x-1;
    }
    return re;
}

int n;
void add(ll C[],int x,ll d)
{
    while(x <= n){
        C[x] += d; x += x&-x;
    }
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int k;
    cin>>n>>k;
    if(k == 0) { cout<<n; return 0;}
    k--;
    ll ans = 0;
    for(int i = 0,j,x; i < n; i++){
        scanf("%d",&x);
        ans += sum(C[k],x);
        for(j = k; j > 0; j--){
            add(C[j],x,sum(C[j-1],x));
        }
        add(C[0],x,1);
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/jerryRey/p/5060590.html