#include <iostream>
using namespace std;
const int N = 5e3;
int main() {
int n;
scanf("%d", &n);
static int o[N + 1];
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
o[a] = i;
}
static int s[N + 1][N + 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
s[i][j] =
s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (i < j && o[i] > o[j]);
auto inv = [&](int i1, int i2, int j1, int j2) {
return s[i2][j2] - s[i1 - 1][j2] - s[i2][j1 - 1] + s[i1 - 1][j1 - 1];
};
auto ninv = [&](int i, int j) {
return (j - i + 1) * (j - i) / 2 - inv(i, j, i, j);
};
static int f[N + 1];
for (int i = 1; i <= n; i++) {
f[i] = ninv(1, i);
for (int j = 1; j < i; j++)
f[i] = min(f[i], f[j] + inv(1, j, j + 1, i) + ninv(j + 1, i));
}
printf("%d\n", f[n]);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:8:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
Main.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d", &a);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
6 ms |
7260 KB |
Output is correct |
32 |
Correct |
6 ms |
7260 KB |
Output is correct |
33 |
Correct |
6 ms |
7256 KB |
Output is correct |
34 |
Correct |
8 ms |
7256 KB |
Output is correct |
35 |
Correct |
6 ms |
7260 KB |
Output is correct |
36 |
Correct |
6 ms |
7372 KB |
Output is correct |
37 |
Correct |
8 ms |
7256 KB |
Output is correct |
38 |
Correct |
7 ms |
7296 KB |
Output is correct |
39 |
Correct |
6 ms |
7256 KB |
Output is correct |
40 |
Correct |
8 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
448 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
1 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
1372 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
6 ms |
7260 KB |
Output is correct |
32 |
Correct |
6 ms |
7260 KB |
Output is correct |
33 |
Correct |
6 ms |
7256 KB |
Output is correct |
34 |
Correct |
8 ms |
7256 KB |
Output is correct |
35 |
Correct |
6 ms |
7260 KB |
Output is correct |
36 |
Correct |
6 ms |
7372 KB |
Output is correct |
37 |
Correct |
8 ms |
7256 KB |
Output is correct |
38 |
Correct |
7 ms |
7296 KB |
Output is correct |
39 |
Correct |
6 ms |
7256 KB |
Output is correct |
40 |
Correct |
8 ms |
7260 KB |
Output is correct |
41 |
Correct |
184 ms |
98284 KB |
Output is correct |
42 |
Correct |
166 ms |
98132 KB |
Output is correct |
43 |
Correct |
179 ms |
98472 KB |
Output is correct |
44 |
Correct |
171 ms |
98324 KB |
Output is correct |
45 |
Correct |
203 ms |
98132 KB |
Output is correct |
46 |
Correct |
174 ms |
98280 KB |
Output is correct |
47 |
Correct |
193 ms |
98344 KB |
Output is correct |
48 |
Correct |
167 ms |
98320 KB |
Output is correct |
49 |
Correct |
180 ms |
98244 KB |
Output is correct |
50 |
Correct |
167 ms |
98344 KB |
Output is correct |
51 |
Correct |
178 ms |
98332 KB |
Output is correct |
52 |
Correct |
171 ms |
98292 KB |
Output is correct |
53 |
Correct |
170 ms |
98344 KB |
Output is correct |
54 |
Correct |
171 ms |
98200 KB |
Output is correct |
55 |
Correct |
209 ms |
98220 KB |
Output is correct |