#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int mxN = 3e5 + 5;
int fact[mxN];
struct FenwickTree{
vector<int>fen;
int sz;
FenwickTree(int n){
sz = n;
fen.resize(n + 1);
}
void Update(int pos){
while(pos <= sz){
fen[pos]++;
pos += (pos & -pos);
}
}
int Sum(int pos){
int sum = 0;
while(pos > 0){
sum += fen[pos];
pos -= (pos & -pos);
}
return sum;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
fact[0] = 1;
for(int i = 1;i < mxN;++i){
fact[i] = (long long)fact[i - 1] * i % MOD;
}
int n;
cin >> n;
vector<int>v(n);
vector<pair<int, int>>compress;
for(int i = 0;i < n;++i){
cin >> v[i];
compress.push_back({v[i], i});
}
sort(compress.begin(), compress.end());
int num = 1;
v[compress[0].second] = num;
FenwickTree tree(n);
for(int i = 1;i < n;++i){
if(compress[i].first != compress[i - 1].first)num++;
v[compress[i].second] = num;
}
int res = 0;
for(int i = 1;i <= n;++i){
int k = v[i - 1] - tree.Sum(v[i - 1]);
res += (long long)fact[n - i] * (k - 1) % MOD;
tree.Update(v[i - 1]);
}
cout << res + 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1628 KB |
Output is correct |
4 |
Correct |
2 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1632 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
2 ms |
1880 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
2 ms |
1636 KB |
Output is correct |
13 |
Correct |
2 ms |
1624 KB |
Output is correct |
14 |
Correct |
2 ms |
1472 KB |
Output is correct |
15 |
Correct |
2 ms |
1624 KB |
Output is correct |
16 |
Correct |
2 ms |
1628 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
1636 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1628 KB |
Output is correct |
2 |
Incorrect |
39 ms |
11128 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
33 ms |
9032 KB |
Output is correct |
3 |
Correct |
32 ms |
10400 KB |
Output is correct |
4 |
Correct |
33 ms |
9608 KB |
Output is correct |
5 |
Correct |
32 ms |
10964 KB |
Output is correct |
6 |
Correct |
32 ms |
9168 KB |
Output is correct |
7 |
Correct |
32 ms |
10708 KB |
Output is correct |
8 |
Correct |
32 ms |
9388 KB |
Output is correct |
9 |
Correct |
32 ms |
8916 KB |
Output is correct |
10 |
Correct |
32 ms |
10452 KB |
Output is correct |
11 |
Correct |
32 ms |
10024 KB |
Output is correct |
12 |
Correct |
33 ms |
10184 KB |
Output is correct |
13 |
Correct |
32 ms |
9928 KB |
Output is correct |
14 |
Correct |
37 ms |
9868 KB |
Output is correct |
15 |
Correct |
37 ms |
10696 KB |
Output is correct |
16 |
Correct |
32 ms |
10524 KB |
Output is correct |
17 |
Correct |
33 ms |
10124 KB |
Output is correct |
18 |
Correct |
34 ms |
10700 KB |
Output is correct |
19 |
Correct |
32 ms |
9684 KB |
Output is correct |
20 |
Correct |
40 ms |
9788 KB |
Output is correct |
21 |
Correct |
32 ms |
9672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1628 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1628 KB |
Output is correct |
4 |
Correct |
2 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1632 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
2 ms |
1880 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
2 ms |
1636 KB |
Output is correct |
13 |
Correct |
2 ms |
1624 KB |
Output is correct |
14 |
Correct |
2 ms |
1472 KB |
Output is correct |
15 |
Correct |
2 ms |
1624 KB |
Output is correct |
16 |
Correct |
2 ms |
1628 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
1636 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
25 |
Correct |
2 ms |
1624 KB |
Output is correct |
26 |
Incorrect |
3 ms |
1628 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
33 ms |
9032 KB |
Output is correct |
3 |
Correct |
32 ms |
10400 KB |
Output is correct |
4 |
Correct |
33 ms |
9608 KB |
Output is correct |
5 |
Correct |
32 ms |
10964 KB |
Output is correct |
6 |
Correct |
32 ms |
9168 KB |
Output is correct |
7 |
Correct |
32 ms |
10708 KB |
Output is correct |
8 |
Correct |
32 ms |
9388 KB |
Output is correct |
9 |
Correct |
32 ms |
8916 KB |
Output is correct |
10 |
Correct |
32 ms |
10452 KB |
Output is correct |
11 |
Correct |
32 ms |
10024 KB |
Output is correct |
12 |
Correct |
33 ms |
10184 KB |
Output is correct |
13 |
Correct |
32 ms |
9928 KB |
Output is correct |
14 |
Correct |
37 ms |
9868 KB |
Output is correct |
15 |
Correct |
37 ms |
10696 KB |
Output is correct |
16 |
Correct |
32 ms |
10524 KB |
Output is correct |
17 |
Correct |
33 ms |
10124 KB |
Output is correct |
18 |
Correct |
34 ms |
10700 KB |
Output is correct |
19 |
Correct |
32 ms |
9684 KB |
Output is correct |
20 |
Correct |
40 ms |
9788 KB |
Output is correct |
21 |
Correct |
32 ms |
9672 KB |
Output is correct |
22 |
Correct |
2 ms |
1624 KB |
Output is correct |
23 |
Incorrect |
3 ms |
1628 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1624 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
2 ms |
1628 KB |
Output is correct |
4 |
Correct |
2 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
1632 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
2 ms |
1628 KB |
Output is correct |
10 |
Correct |
2 ms |
1880 KB |
Output is correct |
11 |
Correct |
2 ms |
1628 KB |
Output is correct |
12 |
Correct |
2 ms |
1636 KB |
Output is correct |
13 |
Correct |
2 ms |
1624 KB |
Output is correct |
14 |
Correct |
2 ms |
1472 KB |
Output is correct |
15 |
Correct |
2 ms |
1624 KB |
Output is correct |
16 |
Correct |
2 ms |
1628 KB |
Output is correct |
17 |
Correct |
2 ms |
1628 KB |
Output is correct |
18 |
Correct |
2 ms |
1628 KB |
Output is correct |
19 |
Correct |
2 ms |
1628 KB |
Output is correct |
20 |
Correct |
2 ms |
1628 KB |
Output is correct |
21 |
Correct |
2 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
1636 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1628 KB |
Output is correct |
25 |
Correct |
2 ms |
1628 KB |
Output is correct |
26 |
Incorrect |
39 ms |
11128 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |