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
#define ll long long
using namespace std;
const int N = 3e5+10;
const int MOD = 1e9+7;
int p[N], a[N];
ll fac[N];
int fenwick[N];
void update(int idx) {
for(; idx < N; idx += (idx&-idx)) {
fenwick[idx]++;
}
}
int query(int idx) {
int res = 0;
for(; idx > 0; idx -= (idx&-idx)) {
res += fenwick[idx];
}
return res;
}
bool cmp(int x, int y) {
return p[x] < p[y];
}
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;
}
for(int i = 1; i <= n; i++) {
cin >> p[i];
a[i] = i;
}
sort(a+1, a+n+1, cmp);
for(int i = 1; i <= n; i++) {
p[a[i]] = i;
}
ll ans = 1;
for(int i = 1; i <= n; i++) {
ans = (ans+fac[n-i]*(p[i]-query(p[i])-1))%MOD;
update(p[i]);
}
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... |