1523. K-inversions(K逆序对)
这题应该说有一些DP的思想吧 dp[i][j]表示以i为结尾第j个数的个数 k单调下降 直接求的话肯定超时 然后用树状数组来进行维护
求k-1次树状数组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 using namespace std; 7 #define lowbit(x) x&(-x) 8 #define N 20010 9 #define LL long long 10 #define mod 1000000000 11 int dp[N][12],n,a[N],po[N],sum[N][12]; 12 void add(int x,int k,int d) 13 { 14 while(x) 15 { 16 dp[x][k] = (d+dp[x][k])%mod; 17 x-=lowbit(x); 18 } 19 } 20 LL getsum(int x,int k) 21 { 22 LL s=0; 23 while(x<=n) 24 { 25 s=(s+dp[x][k])%mod; 26 x+=lowbit(x); 27 } 28 return s; 29 } 30 int main() 31 { 32 int i,j,k; 33 scanf("%d%d",&n,&k); 34 for(i = 1;i <= n ; i++) 35 { 36 scanf("%d",&a[i]); 37 sum[a[i]][1] = 1; 38 } 39 LL s = 0; 40 for(i = 2; i <= k ; i++) 41 { 42 for(j = i-1; j <= n ;j++) 43 { 44 add(a[j],i,sum[a[j]][i-1]); 45 sum[a[j]][i] = getsum(a[j]+1,i); 46 } 47 } 48 for(i = 1 ; i <= n ; i++) 49 s = (s+sum[a[i]][k])%mod; 50 printf("%lld\n",s); 51 return 0; 52 }