Problem Description
Tom has learned how to calculate the number of inversions in a permutation of n distinct objects by coding, his teacher gives him a problem:
Give you a permutation of n distinct integer from 1 to n, there are many permutations of 1-n is smaller than the given permutation on dictionary order, and for each of them, you can calculate the number of inversions by coding. You need to find out sum total of them.
Tom doesn’t know how to do, he wants you to help him.
Because the number may be very large, output the answer to the problem modulo 109+7.
Input
Multi test cases(about 20). For each case, the first line contains a positive integer n, the second line contains n integers, it’s a permutation of 1-n.
n≤100
Output
For each case, print one line, the answer to the problem modulo 109+7.
Sample Input
3
2 1 3
5
2 1 4 3 5
Sample Output
1
75
Hint
The input may be very big, we might as well optimize input.
Source
BestCoder Round #40
Recommend
首先,n个数的全排列的逆序数有规律,可以递推
然后枚举第k位开始变小,然后后面的就是全排列了
然后考虑k-1位以前的逆序数还有前k-1位对k+1位以后的数产生的逆序数,第k位往前往后分别产生的逆序数,乘上个阶乘
/*************************************************************************
> File Name: hdu5225.cpp
> Author: ALex
> Created Time: 2015年05月09日 星期六 21时15分54秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
static const int mod = 1e9 + 7;
LL per_inver[110];
int arr[110];
bool vis[110];
LL sum[110];
LL f[110];
int tree[110];
int lowbit(int x) {
return x & (-x);
}
void add(int x, int n) {
for (int i = x; i <= n; i += lowbit(i)) {
++tree[i];
}
}
int getsum(int x) {
int ans = 0;
for (int i = x; i; i -= lowbit(i)) {
ans += tree[i];
}
return ans;
}
int main() {
per_inver[0] = 0;
per_inver[1] = 0;
f[0] = 1;
for (int i = 1; i <= 100; ++i) {
f[i] = f[i - 1] * i;
f[i] %= mod;
}
LL jiechen = 1;
for (int i = 2; i <= 100; ++i) {
LL last = per_inver[i - 1];
jiechen *= (i - 1);
jiechen %= mod;
per_inver[i] = 0;
for (int j = 0; j < i; ++j) {
per_inver[i] += last;
per_inver[i] %= mod;
last += jiechen;
last %= mod;
}
}
int n;
while (~scanf("%d", &n)) {
for (int i = 1;i <= n; ++i) {
scanf("%d", &arr[i]);
}
memset(tree, 0, sizeof(tree));
sum[0] = 0;
for (int i = 1; i <= n; ++i) {
add(arr[i], n);
LL cur = i - getsum(arr[i]);
cur %= mod;
sum[i] = sum[i - 1] + cur;
sum[i] %= mod;
}
LL ans = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < arr[i]; ++j) { //这一位开始小,前面的都相同,枚举没出现过的
LL pre = sum[i - 1]; //前面固定的逆序对
if (!vis[j]) {
LL cur = 0, u = 0;
for (int k = 1; k < j; ++k) {
if (!vis[k]) {
++u;
}
}
for (int k = j + 1; k <= n; ++k) {
if (vis[k]) {
++cur;
}
}
int v = 0;
for (int k = 1; k < i; ++k) {
for (int l = i; l <= n; ++l) {
if (!vis[arr[l]] && arr[l] != j) {
if (arr[k] > arr[l]) {
++v;
}
}
}
}
ans += per_inver[n - i] + (cur + u + v + pre) * f[n - i];
ans %= mod;
}
}
vis[arr[i]] = 1;
}
printf("%lld\n", ans);
}
return 0;
}