题目链接
计算排列的编号
题解
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
vector<ll> f(21);
void init() {
f[0] = 1;
for (int i = 1; i <= 20; i++) f[i] = f[i - 1] * i;
}
int main(int argc, char const* argv[]) {
init();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> vis(n + 1, false);
ll skip = 0;
for (int i = 0; i < n; i++) {
int count = 0;
for (int k = 1; k < a[i]; k++)
if (!vis[k]) count++;
skip += count * f[n - i - 1];
vis[a[i]] = true;
}
cout << skip + 1 << endl;
}
return 0;
}