#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int val;
int rank;
bool operator<(const node &r1) const
{
return val < r1.val;
}
};
node lisan[100005];
int arr[100005], haofan[100005];
long long ans[10][100005];
int n, k;
bool cmp(node &a, node &b)
{
return a.val > b.val;
}
long long Query(int x, int row) {//查询1~x的区间和
long long res = 0;
for (int i = x; i; i -= (i & -i))
{
res += ans[row][i];
res %= 1000000007;
}
return res;
}
void Add(int x,int row, long long p) { //在x处增加p的值
for (int i = x; i <= n; i += (i & -i))
{
ans[row][i] += p;
ans[row][i] %= 1000000007;
}
if (row != k-1 && x!= 1 && Query(x-1, row) > 0)
Add(x, row + 1, Query(x-1, row));
}
int main()
{
cin.tie(0);
cin >> n >> k;
int num, i, j;
for (int i = 1; i <= n; ++i)
{
cin >> lisan[i].val;
lisan[i].rank = i;
}
stable_sort(lisan + 1, lisan + n+ 1);
//现在离散的从头到尾的顺序 val是从小到大排列的,
for (int i = 1; i <= n; ++i)
{
arr[lisan[i].rank] = i; //现在arr是离散好的数组;
}
//arr[1] = 4; arr[2] = 2; arr[3] = 1; arr[4] = 3;
for (i = n; i >0; --i)
{
Add(arr[i],0, 1);
}
cout << Query(n, k-1);
return 0;
}
k阶逆序对
最新推荐文章于 2020-12-01 21:48:47 发布