1523. 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 and ai1 > 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
input | output |
---|---|
3 2 3 1 2 |
2 |
5 3 5 4 3 2 1 |
10 |
Problem Author: Dmitry Gozman Problem Source: Dmitry Gozman Contest 1, Petrozavodsk training camp, January
************************************************************************************************
树状数组(加快运算时间)
************************************************************************************************
1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 const int N=20005; 9 const int MOD=1000000000; 10 int a[N],dp[2][N]; 11 int c[N]; 12 int i,j,k,n; 13 int sum; 14 int lowbit(int x)//低位技术 15 { 16 return x&(-x); 17 } 18 void insert(int x,int v)//修改 19 { 20 for(;x<=n;x+=lowbit(x)) 21 c[x]=(c[x]+v)%MOD; 22 } 23 int query(int x)//查询 24 { 25 int suma=0; 26 for(;x>0;x-=lowbit(x)) 27 suma=(suma+c[x])%MOD; 28 return suma; 29 } 30 int main() 31 { 32 cin>>n>>k; 33 for(i=1;i<=n;i++) 34 { 35 cin>>a[i]; 36 dp[1][i]=1; 37 } 38 int now=0; 39 for(i=1;i<k;i++,now^=1) 40 { 41 memset(c,0,sizeof(c)); 42 sum=0; 43 for(j=i;j<=n;j++) 44 { 45 sum=(sum+dp[now^1][j])%MOD;//滚动数组 46 insert(a[j],dp[now^1][j]);//满足dp[i][j]=dp[i-1][i……n] 47 int temp=query(a[j]); 48 if(sum<=temp)sum+=MOD; 49 dp[now][j]=sum-temp; 50 if(sum>MOD)sum-=MOD; 51 52 } 53 } 54 sum=0; 55 for(i=k;i<=n;i++) 56 sum=(sum+dp[now^1][i])%MOD; 57 if(sum>MOD)sum-=MOD; 58 cout<<sum<<endl; 59 return 0; 60 61 62 }