#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 5001;
int h[N];
int h_i[N];
int dp[N][N];
int dp2[N];
struct BIT{
int bit[N];
BIT() = default;
BIT(int n){
fill(bit, bit + n, 0);
}
void clear(int n){
fill(bit, bit + n, 0);
}
void upd(int x, int v){
for(int i = x; i < N; i = i | (i + 1)){
bit[i] += v;
}
}
int qry(int x){
int ret = 0;
for(int i = x; i >= 0; i = (i & (i + 1)) - 1){
ret += bit[i];
}
return ret;
}
};
int32_t main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> h[i];
h_i[h[i]] = i;
}
for(int sm = 1; sm <= n; sm++){
BIT bit(n);
int cnt;
cnt = 0;
for(int i = sm; i <= n; i++){
bit.upd(h_i[i], 1);
}
for(int i = sm; i <= n; i++){
cnt += bit.qry(h_i[i] - 1);
bit.upd(h_i[i], -1);
dp[i][sm] = cnt;
}
bit.clear(n);
cnt = 0;
for(int i = sm; i <= n; i++){
cnt += bit.qry(h_i[i] - 1);
bit.upd(h_i[i], 1);
dp[i][sm] += cnt;
}
bit.clear(n);
cnt = 0;
for(int i = sm; i <= n; i++){
cnt += (i - sm) - bit.qry(h_i[i]);
bit.upd(h_i[i], 1);
dp[i][sm] -= cnt;
}
}
fill(dp2 + 1, dp2 + n + 1, (1 << 30));
for(int i = 0; i < n; i++){
for(int j = 0; j <= i; j++){
dp2[i + 1] = min(dp2[i + 1], dp2[j] + dp[i + 1][j + 1]);
}
}
cout << dp2[n] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1092 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1220 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1092 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1220 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
31 |
Correct |
24 ms |
4708 KB |
Output is correct |
32 |
Correct |
26 ms |
4788 KB |
Output is correct |
33 |
Correct |
24 ms |
4808 KB |
Output is correct |
34 |
Correct |
27 ms |
4744 KB |
Output is correct |
35 |
Correct |
24 ms |
4696 KB |
Output is correct |
36 |
Correct |
24 ms |
4820 KB |
Output is correct |
37 |
Correct |
25 ms |
4816 KB |
Output is correct |
38 |
Correct |
24 ms |
4692 KB |
Output is correct |
39 |
Correct |
25 ms |
4808 KB |
Output is correct |
40 |
Correct |
24 ms |
4736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
324 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 |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1092 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
2 ms |
1220 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
3 ms |
1108 KB |
Output is correct |
31 |
Correct |
24 ms |
4708 KB |
Output is correct |
32 |
Correct |
26 ms |
4788 KB |
Output is correct |
33 |
Correct |
24 ms |
4808 KB |
Output is correct |
34 |
Correct |
27 ms |
4744 KB |
Output is correct |
35 |
Correct |
24 ms |
4696 KB |
Output is correct |
36 |
Correct |
24 ms |
4820 KB |
Output is correct |
37 |
Correct |
25 ms |
4816 KB |
Output is correct |
38 |
Correct |
24 ms |
4692 KB |
Output is correct |
39 |
Correct |
25 ms |
4808 KB |
Output is correct |
40 |
Correct |
24 ms |
4736 KB |
Output is correct |
41 |
Correct |
973 ms |
67420 KB |
Output is correct |
42 |
Correct |
989 ms |
67264 KB |
Output is correct |
43 |
Correct |
1024 ms |
67432 KB |
Output is correct |
44 |
Correct |
972 ms |
67416 KB |
Output is correct |
45 |
Correct |
965 ms |
67416 KB |
Output is correct |
46 |
Correct |
977 ms |
67272 KB |
Output is correct |
47 |
Correct |
969 ms |
67520 KB |
Output is correct |
48 |
Correct |
965 ms |
67400 KB |
Output is correct |
49 |
Correct |
989 ms |
67356 KB |
Output is correct |
50 |
Correct |
994 ms |
67292 KB |
Output is correct |
51 |
Correct |
981 ms |
67264 KB |
Output is correct |
52 |
Correct |
984 ms |
67320 KB |
Output is correct |
53 |
Correct |
978 ms |
67396 KB |
Output is correct |
54 |
Correct |
967 ms |
67352 KB |
Output is correct |
55 |
Correct |
980 ms |
67272 KB |
Output is correct |