This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int N = 3e5 + 10;
const int MOD = 1e9 + 7;
int p[N];
map <int, int> cmp;
int fenwick[N];
long long fac[N];
void update(int idx, int val) {
for(; idx < N; idx += (idx & -idx)) {
fenwick[idx] += val;
}
}
int query(int idx) {
int res = 0;
for(; idx > 0; idx -= (idx & -idx)) {
res += fenwick[idx];
}
return res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
fac[0] = 1;
for(int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i % MOD;
}
set <int> s;
for(int i = 1; i <= n; i++) {
cin >> p[i];
s.insert(p[i]);
}
int cnt = 0;
for(auto v : s) {
cmp[v] = ++cnt;
}
long long ans = 1;
for(int i = 1; i <= n; i++) {
ans = (ans + fac[n - i] * (cmp[p[i]] - query(cmp[p[i]]) - 1) % MOD) % MOD;
update(cmp[p[i]], 1);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |