#include <bits/stdc++.h>
using namespace std;
const int N = 5003;
int f[N];
int cst[N][N];
int dp[N];
int lsb(int i){
return i&-i;
}
void upd(int ind, int val){
for(int i=ind; i<N; i+=lsb(i))f[i]+=val;
}
int get(int l){
int ret=0;
for(int i=l; i>0; i-=lsb(i))ret+=f[i];
return ret;
}
int get(int l, int r){
return get(r)-get(l-1);
}
void calccost(int a[], int n){
for(int i=1; i<=n; i++){
int sz=n-i+1;
int pos[n+1];
int b[sz];
int skip=0;
for(int j=0; j< n; j++){
if(a[j]<i)skip++;
else b[j-skip]=a[j];
pos[a[j]]=j-skip+1;
}
int numinv=0;
int sumpos=0;
for(int j=i; j<=n; j++){
upd(pos[j], 1);
numinv+=get(0, pos[j]-1);
sumpos+=pos[j];
sumpos-=j-i+1;
cst[i][j]=numinv+sumpos;
}
for(int j=0; j<N; j++){
f[j]=0;
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int a[n];
for(int i=0; i< n; i++)cin >> a[i];
calccost(a, n);
for(int i=1; i<=n; i++){
dp[i]=1e9;
for(int j=i-1; j>=0; j--){
dp[i]=min(dp[i], dp[j]+cst[j+1][i]);
}
}
cout << dp[n] << '\n';
}
Compilation message
Main.cpp: In function 'void calccost(int*, int)':
Main.cpp:32:13: warning: variable 'b' set but not used [-Wunused-but-set-variable]
32 | int b[sz];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2508 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2488 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2508 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2488 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
2 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2508 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2488 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
2 ms |
4564 KB |
Output is correct |
31 |
Correct |
10 ms |
16732 KB |
Output is correct |
32 |
Correct |
10 ms |
16884 KB |
Output is correct |
33 |
Correct |
9 ms |
16848 KB |
Output is correct |
34 |
Correct |
10 ms |
16732 KB |
Output is correct |
35 |
Correct |
9 ms |
16940 KB |
Output is correct |
36 |
Correct |
10 ms |
16732 KB |
Output is correct |
37 |
Correct |
9 ms |
16732 KB |
Output is correct |
38 |
Correct |
9 ms |
16732 KB |
Output is correct |
39 |
Correct |
10 ms |
16944 KB |
Output is correct |
40 |
Correct |
9 ms |
16732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2508 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2488 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4440 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
2 ms |
4564 KB |
Output is correct |
31 |
Correct |
10 ms |
16732 KB |
Output is correct |
32 |
Correct |
10 ms |
16884 KB |
Output is correct |
33 |
Correct |
9 ms |
16848 KB |
Output is correct |
34 |
Correct |
10 ms |
16732 KB |
Output is correct |
35 |
Correct |
9 ms |
16940 KB |
Output is correct |
36 |
Correct |
10 ms |
16732 KB |
Output is correct |
37 |
Correct |
9 ms |
16732 KB |
Output is correct |
38 |
Correct |
9 ms |
16732 KB |
Output is correct |
39 |
Correct |
10 ms |
16944 KB |
Output is correct |
40 |
Correct |
9 ms |
16732 KB |
Output is correct |
41 |
Correct |
330 ms |
97280 KB |
Output is correct |
42 |
Correct |
327 ms |
97360 KB |
Output is correct |
43 |
Correct |
358 ms |
97360 KB |
Output is correct |
44 |
Correct |
327 ms |
97364 KB |
Output is correct |
45 |
Correct |
328 ms |
97240 KB |
Output is correct |
46 |
Correct |
334 ms |
97572 KB |
Output is correct |
47 |
Correct |
329 ms |
97212 KB |
Output is correct |
48 |
Correct |
326 ms |
97360 KB |
Output is correct |
49 |
Correct |
332 ms |
97364 KB |
Output is correct |
50 |
Correct |
329 ms |
97200 KB |
Output is correct |
51 |
Correct |
327 ms |
97224 KB |
Output is correct |
52 |
Correct |
332 ms |
97468 KB |
Output is correct |
53 |
Correct |
329 ms |
97344 KB |
Output is correct |
54 |
Correct |
334 ms |
97364 KB |
Output is correct |
55 |
Correct |
328 ms |
97364 KB |
Output is correct |