#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[5005];
int pos[5005];
int cost[5005][5005];
int fw[5005];
int fw2[5005];
int dp[5005];
void up(int *f, int x){
while (x <= n){
f[x]++;
x += x&-x;
}
}
int qu(int *f, int x){
int ret = 0;
while (x){
ret += f[x];
x -= x&-x;
}
return ret;
}
main(){
scanf("%lld",&n);
for (int i = 1; i <= n; i++) {
scanf("%lld",&a[i]);
pos[a[i]] = i;
}
for (int i = 1; i <= n; i++){
int pos1 = 0;
int pos2 = 0;
for (int j = 1; j <= n; j++){
fw[j] = 0;
}
int num_inv = 0;
for (int j = i; j <= n; j++){
int realpos = pos[j] + (i-1)-qu(fw2, pos[j]);
pos1 += realpos;
pos2 += j;
num_inv += qu(fw, pos[j]);
cost[i][j] = pos1-pos2 + num_inv;
///printf("cost %lld %lld = %lld (realpos = %lld, pos = %lld)\n",i,j,cost[i][j],realpos, j);
up(fw,pos[j]);
}
up(fw2, pos[i]);
}
dp[0] = 0;
for (int i = 1; i <= n; i++){
dp[i] = 1000000000000000000;
for (int j = 1; j <= i; j++){
///printf("dp[%lld] = %lld, + %lld\n",j-1,dp[j-1],cost[j][i]);
dp[i] = min(dp[i], dp[j-1]+cost[j][i]);
}
///printf("dp[%lld] = %lld\n",i,dp[i]);
}
printf("%lld",dp[n]);
}
Compilation message
Main.cpp:26:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
26 | main(){
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
Main.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%lld",&a[i]);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1204 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 |
1204 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1200 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1204 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 |
1204 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1200 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
10 ms |
5972 KB |
Output is correct |
32 |
Correct |
10 ms |
5972 KB |
Output is correct |
33 |
Correct |
12 ms |
6100 KB |
Output is correct |
34 |
Correct |
13 ms |
6100 KB |
Output is correct |
35 |
Correct |
11 ms |
6100 KB |
Output is correct |
36 |
Correct |
10 ms |
6064 KB |
Output is correct |
37 |
Correct |
10 ms |
6100 KB |
Output is correct |
38 |
Correct |
10 ms |
6100 KB |
Output is correct |
39 |
Correct |
10 ms |
6100 KB |
Output is correct |
40 |
Correct |
11 ms |
6068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
2 ms |
1204 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 |
1204 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1200 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
10 ms |
5972 KB |
Output is correct |
32 |
Correct |
10 ms |
5972 KB |
Output is correct |
33 |
Correct |
12 ms |
6100 KB |
Output is correct |
34 |
Correct |
13 ms |
6100 KB |
Output is correct |
35 |
Correct |
11 ms |
6100 KB |
Output is correct |
36 |
Correct |
10 ms |
6064 KB |
Output is correct |
37 |
Correct |
10 ms |
6100 KB |
Output is correct |
38 |
Correct |
10 ms |
6100 KB |
Output is correct |
39 |
Correct |
10 ms |
6100 KB |
Output is correct |
40 |
Correct |
11 ms |
6068 KB |
Output is correct |
41 |
Correct |
537 ms |
117376 KB |
Output is correct |
42 |
Correct |
519 ms |
117380 KB |
Output is correct |
43 |
Correct |
533 ms |
117456 KB |
Output is correct |
44 |
Correct |
561 ms |
117552 KB |
Output is correct |
45 |
Correct |
496 ms |
117364 KB |
Output is correct |
46 |
Correct |
558 ms |
117572 KB |
Output is correct |
47 |
Correct |
591 ms |
117592 KB |
Output is correct |
48 |
Correct |
518 ms |
117452 KB |
Output is correct |
49 |
Correct |
531 ms |
117580 KB |
Output is correct |
50 |
Correct |
556 ms |
117608 KB |
Output is correct |
51 |
Correct |
515 ms |
117496 KB |
Output is correct |
52 |
Correct |
557 ms |
117576 KB |
Output is correct |
53 |
Correct |
515 ms |
117664 KB |
Output is correct |
54 |
Correct |
570 ms |
117496 KB |
Output is correct |
55 |
Correct |
528 ms |
117452 KB |
Output is correct |