#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5005;
int n, q, a[N], inv[N][N], invr[N][N], S[N], Dp[N];
main () {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
for (int j = a[i]; j <= n; ++j) {
++S[j];
}
for (int x = a[i] + 1; x <= n; ++x) {
inv[a[i]][x] = S[n] - S[x - 1];
invr[a[i]][x] = (x - a[i]) - (S[x] - S[a[i]]);
}
}
ll U;
for (int r = 1; r <= n; ++r) {
Dp[r] = 1e9, U = 0;
for (int l = r; l >= 1; --l) {
U += inv[l][r + 1] + invr[l][r];
if (Dp[r] > Dp[l - 1] + U)
Dp[r] = Dp[l - 1] + U;
}
}
cout << Dp[n] << "\n";
}
Compilation message
Main.cpp:11:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main () {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2156 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
2 ms |
2156 KB |
Output is correct |
25 |
Correct |
2 ms |
2156 KB |
Output is correct |
26 |
Correct |
2 ms |
2156 KB |
Output is correct |
27 |
Correct |
2 ms |
2156 KB |
Output is correct |
28 |
Correct |
1 ms |
2156 KB |
Output is correct |
29 |
Correct |
2 ms |
2156 KB |
Output is correct |
30 |
Correct |
1 ms |
2156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2156 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
2 ms |
2156 KB |
Output is correct |
25 |
Correct |
2 ms |
2156 KB |
Output is correct |
26 |
Correct |
2 ms |
2156 KB |
Output is correct |
27 |
Correct |
2 ms |
2156 KB |
Output is correct |
28 |
Correct |
1 ms |
2156 KB |
Output is correct |
29 |
Correct |
2 ms |
2156 KB |
Output is correct |
30 |
Correct |
1 ms |
2156 KB |
Output is correct |
31 |
Correct |
9 ms |
9324 KB |
Output is correct |
32 |
Correct |
9 ms |
9324 KB |
Output is correct |
33 |
Correct |
9 ms |
9324 KB |
Output is correct |
34 |
Correct |
9 ms |
9324 KB |
Output is correct |
35 |
Correct |
11 ms |
9324 KB |
Output is correct |
36 |
Correct |
9 ms |
9324 KB |
Output is correct |
37 |
Correct |
10 ms |
9324 KB |
Output is correct |
38 |
Correct |
9 ms |
9324 KB |
Output is correct |
39 |
Correct |
9 ms |
9324 KB |
Output is correct |
40 |
Correct |
9 ms |
9324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2156 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
2 ms |
2156 KB |
Output is correct |
25 |
Correct |
2 ms |
2156 KB |
Output is correct |
26 |
Correct |
2 ms |
2156 KB |
Output is correct |
27 |
Correct |
2 ms |
2156 KB |
Output is correct |
28 |
Correct |
1 ms |
2156 KB |
Output is correct |
29 |
Correct |
2 ms |
2156 KB |
Output is correct |
30 |
Correct |
1 ms |
2156 KB |
Output is correct |
31 |
Correct |
9 ms |
9324 KB |
Output is correct |
32 |
Correct |
9 ms |
9324 KB |
Output is correct |
33 |
Correct |
9 ms |
9324 KB |
Output is correct |
34 |
Correct |
9 ms |
9324 KB |
Output is correct |
35 |
Correct |
11 ms |
9324 KB |
Output is correct |
36 |
Correct |
9 ms |
9324 KB |
Output is correct |
37 |
Correct |
10 ms |
9324 KB |
Output is correct |
38 |
Correct |
9 ms |
9324 KB |
Output is correct |
39 |
Correct |
9 ms |
9324 KB |
Output is correct |
40 |
Correct |
9 ms |
9324 KB |
Output is correct |
41 |
Correct |
401 ms |
134252 KB |
Output is correct |
42 |
Correct |
403 ms |
134396 KB |
Output is correct |
43 |
Correct |
401 ms |
134516 KB |
Output is correct |
44 |
Correct |
393 ms |
134252 KB |
Output is correct |
45 |
Correct |
402 ms |
134380 KB |
Output is correct |
46 |
Correct |
407 ms |
134380 KB |
Output is correct |
47 |
Correct |
398 ms |
134504 KB |
Output is correct |
48 |
Correct |
402 ms |
134656 KB |
Output is correct |
49 |
Correct |
402 ms |
134636 KB |
Output is correct |
50 |
Correct |
404 ms |
134508 KB |
Output is correct |
51 |
Correct |
403 ms |
134380 KB |
Output is correct |
52 |
Correct |
399 ms |
134380 KB |
Output is correct |
53 |
Correct |
401 ms |
134380 KB |
Output is correct |
54 |
Correct |
395 ms |
134380 KB |
Output is correct |
55 |
Correct |
417 ms |
134508 KB |
Output is correct |