#include <bits/stdc++.h>
using namespace std;
int n, a[5001], dp[5001], pos[5001], inv[5001][5001], calc2[5001][5001];
struct BIT {
int tree[5001];
void add (int x) {
for (; x <= 5000; x += x & (-x)) tree[x]++;
}
int get (int x) {
int sum = 0;
for (; x > 0; x -= x & (-x)) sum += tree[x];
return sum;
}
} cur;
int main () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i;
for (int i = 1; i <= n; i++) {
memset(cur.tree, 0, sizeof(cur.tree));
for (int j = i; j <= n; j++) {
inv[i][j] = cur.get(pos[j] - 1) + inv[i][j - 1];
cur.add(pos[j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = i - 1; j >= 1; j--) {
if (a[j] > a[i]) {
calc2[a[i] + 1][a[j]]++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
calc2[j][i] += calc2[j - 1][i];
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
calc2[i][j] += calc2[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
dp[i] = 1e9;
for (int j = i; j >= 1; j--) {
int sum = calc2[j][i] - calc2[j][j - 1];
dp[i] = min(dp[i], dp[j - 1] + sum + inv[j][i]);
}
}
cout << dp[n] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
5464 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
2 ms |
5468 KB |
Output is correct |
26 |
Correct |
2 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
5468 KB |
Output is correct |
28 |
Correct |
2 ms |
5464 KB |
Output is correct |
29 |
Correct |
2 ms |
5468 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
5464 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
2 ms |
5468 KB |
Output is correct |
26 |
Correct |
2 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
5468 KB |
Output is correct |
28 |
Correct |
2 ms |
5464 KB |
Output is correct |
29 |
Correct |
2 ms |
5468 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
12 ms |
21340 KB |
Output is correct |
32 |
Correct |
12 ms |
21364 KB |
Output is correct |
33 |
Correct |
11 ms |
21340 KB |
Output is correct |
34 |
Correct |
13 ms |
21340 KB |
Output is correct |
35 |
Correct |
13 ms |
21340 KB |
Output is correct |
36 |
Correct |
13 ms |
21444 KB |
Output is correct |
37 |
Correct |
13 ms |
21296 KB |
Output is correct |
38 |
Correct |
12 ms |
21220 KB |
Output is correct |
39 |
Correct |
15 ms |
21340 KB |
Output is correct |
40 |
Correct |
12 ms |
21432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
412 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
5464 KB |
Output is correct |
22 |
Correct |
2 ms |
5468 KB |
Output is correct |
23 |
Correct |
2 ms |
5468 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
2 ms |
5468 KB |
Output is correct |
26 |
Correct |
2 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
5468 KB |
Output is correct |
28 |
Correct |
2 ms |
5464 KB |
Output is correct |
29 |
Correct |
2 ms |
5468 KB |
Output is correct |
30 |
Correct |
2 ms |
5468 KB |
Output is correct |
31 |
Correct |
12 ms |
21340 KB |
Output is correct |
32 |
Correct |
12 ms |
21364 KB |
Output is correct |
33 |
Correct |
11 ms |
21340 KB |
Output is correct |
34 |
Correct |
13 ms |
21340 KB |
Output is correct |
35 |
Correct |
13 ms |
21340 KB |
Output is correct |
36 |
Correct |
13 ms |
21444 KB |
Output is correct |
37 |
Correct |
13 ms |
21296 KB |
Output is correct |
38 |
Correct |
12 ms |
21220 KB |
Output is correct |
39 |
Correct |
15 ms |
21340 KB |
Output is correct |
40 |
Correct |
12 ms |
21432 KB |
Output is correct |
41 |
Correct |
443 ms |
174408 KB |
Output is correct |
42 |
Correct |
437 ms |
174468 KB |
Output is correct |
43 |
Correct |
425 ms |
174348 KB |
Output is correct |
44 |
Correct |
422 ms |
174368 KB |
Output is correct |
45 |
Correct |
420 ms |
174512 KB |
Output is correct |
46 |
Correct |
426 ms |
174352 KB |
Output is correct |
47 |
Correct |
425 ms |
174688 KB |
Output is correct |
48 |
Correct |
427 ms |
174332 KB |
Output is correct |
49 |
Correct |
424 ms |
174476 KB |
Output is correct |
50 |
Correct |
433 ms |
174476 KB |
Output is correct |
51 |
Correct |
443 ms |
174324 KB |
Output is correct |
52 |
Correct |
423 ms |
174680 KB |
Output is correct |
53 |
Correct |
429 ms |
174420 KB |
Output is correct |
54 |
Correct |
414 ms |
174676 KB |
Output is correct |
55 |
Correct |
441 ms |
174420 KB |
Output is correct |