#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 5e3+5;
int bit[N][2], inv[N][N];
void update(int idx, int pos, int val){
while(pos < N){
bit[pos][idx] += val;
pos += pos&-pos;
}
}
int query(int idx, int pos){
int res = 0;
while(pos){
res += bit[pos][idx];
pos -= pos&-pos;
}
return res;
}
signed main(){
fastio
int n;
cin >> n;
int pos[n+1] = {};
for(int i = 1;i <= n;i++){
int x;
cin >> x;
pos[x] = i;
}
int arr[n+1];
iota(arr,arr+n+1,0);
for(int i = 1;i <= n;i++){
arr[i] = pos[arr[i]];
}
for(int i = 1;i <= n;i++){
for(int j = 0;j <= n;j++){
bit[j][0] = 0;
}
for(int j = i;j <= n;j++){
inv[i][j] = inv[i][j-1] + query(0,arr[j]) + query(1,N-1) - query(1,arr[j]);
update(0,arr[j],1);
}
update(1,arr[i],1);
}
int dp[n+1] = {};
fill(dp+1,dp+n+1,1e18);
for(int i = 1;i <= n;i++){
for(int j = 0;j < i;j++){
dp[i] = min(dp[i],dp[j] + inv[j+1][i]);
//inv => inversions after reverse
}
}
cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1236 KB |
Output is correct |
31 |
Correct |
15 ms |
6012 KB |
Output is correct |
32 |
Correct |
15 ms |
6100 KB |
Output is correct |
33 |
Correct |
16 ms |
6080 KB |
Output is correct |
34 |
Correct |
18 ms |
6128 KB |
Output is correct |
35 |
Correct |
18 ms |
6100 KB |
Output is correct |
36 |
Correct |
17 ms |
6100 KB |
Output is correct |
37 |
Correct |
17 ms |
6100 KB |
Output is correct |
38 |
Correct |
15 ms |
6016 KB |
Output is correct |
39 |
Correct |
14 ms |
6100 KB |
Output is correct |
40 |
Correct |
15 ms |
6112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1236 KB |
Output is correct |
30 |
Correct |
2 ms |
1236 KB |
Output is correct |
31 |
Correct |
15 ms |
6012 KB |
Output is correct |
32 |
Correct |
15 ms |
6100 KB |
Output is correct |
33 |
Correct |
16 ms |
6080 KB |
Output is correct |
34 |
Correct |
18 ms |
6128 KB |
Output is correct |
35 |
Correct |
18 ms |
6100 KB |
Output is correct |
36 |
Correct |
17 ms |
6100 KB |
Output is correct |
37 |
Correct |
17 ms |
6100 KB |
Output is correct |
38 |
Correct |
15 ms |
6016 KB |
Output is correct |
39 |
Correct |
14 ms |
6100 KB |
Output is correct |
40 |
Correct |
15 ms |
6112 KB |
Output is correct |
41 |
Correct |
682 ms |
117444 KB |
Output is correct |
42 |
Correct |
688 ms |
117556 KB |
Output is correct |
43 |
Correct |
674 ms |
117388 KB |
Output is correct |
44 |
Correct |
669 ms |
117416 KB |
Output is correct |
45 |
Correct |
677 ms |
117484 KB |
Output is correct |
46 |
Correct |
664 ms |
117532 KB |
Output is correct |
47 |
Correct |
661 ms |
117520 KB |
Output is correct |
48 |
Correct |
672 ms |
117536 KB |
Output is correct |
49 |
Correct |
677 ms |
117564 KB |
Output is correct |
50 |
Correct |
684 ms |
117600 KB |
Output is correct |
51 |
Correct |
673 ms |
117588 KB |
Output is correct |
52 |
Correct |
659 ms |
117536 KB |
Output is correct |
53 |
Correct |
709 ms |
117532 KB |
Output is correct |
54 |
Correct |
658 ms |
117496 KB |
Output is correct |
55 |
Correct |
679 ms |
117536 KB |
Output is correct |