ural1523 K-inversions

K-inversions

Time limit: 1.0 second
Memory limit: 64 MB
Consider a permutation a1, a2, …, an (all ai are different integers in range from 1 to n). Let us call k-inversion a sequence of numbers i1, i2, …, ik such that 1 ≤ i1 < i2 < … < ik ≤ n andai1 > ai2 > … > aik. Your task is to evaluate the number of different k-inversions in a given permutation.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 20000, 2 ≤ k ≤ 10). The second line is filled with n numbers ai.

Output

Output a single number — the number of k-inversions in a given permutation. The number must be taken modulo 109.

Samples

inputoutput
3 2
3 1 2
2
5 3
5 4 3 2 1
10

 

分析:类似于逆序对,求k次递减序列的个数,树状数组+dp更新,注意取模;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000000
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e4+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,a[maxn],dp[11][maxn],p[maxn];
ll ans;
void add(int x,int y)
{
    for(int i=x;i<=n;i+=(i&(-i)))
        a[i]+=y,a[i]%=mod;
}

int get(int x)
{
    int res=0;
    for(int i=x;i;i-=(i&(-i)))
        res+=a[i],res%=mod;
    return res;
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&k);
    rep(i,1,n)scanf("%d",&p[i]),dp[1][i]=1;
    rep(i,2,k)
    {
        memset(a,0,sizeof a);
        rep(j,1,n)
        {
            dp[i][j]=(get(n)-get(p[j])+mod)%mod;
            add(p[j],dp[i-1][j]);
        }
    }
    rep(i,1,n)ans=(ans+dp[k][i])%mod;
    printf("%lld\n",ans);
    //system("Pause");
    return 0;
}
posted @ 2016-08-25 13:00  mxzf0213  阅读(336)  评论(0编辑  收藏  举报