#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define lli long long int
#define vi vector<int>
#define vlli vector<long long int>
#define pii pair<int, int>
#define plli pair<lli, lli>
#define rep(i, a, b) for(lli i = (a); i <= (b); i++)
#define repa(i, a, b) for(lli i = (a); i >= (b); i--)
#define repv(x, v) for(auto x : v)
#define debug(x) cout << #x << " = " << x << endl
#define debugsl(x) cout << #x << " = " << x << ", "
#define debugarr(x, a, b) cout << #x << " = ["; rep(ii, a, b) cout << x[ii] << ", "; cout << "]\n"
#define pb push_back
#define nl "\n"
#define MAX_N 5002
#define INF (1ll << 62)
#define menoresderecha(x, pos) ((x - 1) - menores[x][pos])
lli n, h[MAX_N], dp[MAX_N], pos[MAX_N], mov[MAX_N][MAX_N], c[MAX_N][MAX_N], menores[MAX_N][MAX_N], mayores[MAX_N][MAX_N];
int main()
{
fastio;
cin >> n;
rep(i, 1, n){ cin >> h[i]; pos[h[i]] = i; }
rep(i, 1, n){
rep(j, 1, n){
mayores[j][i] = mayores[j][i - 1];
menores[j][i] = menores[j][i - 1];
}
rep(j, h[i] + 1, n) ++menores[j][i];
repa(j, h[i] - 1, 1) ++mayores[j][i];
}
rep(i, 1, n){
rep(j, i + 1, n){
mov[i][j] = menores[j][pos[j]] - menores[i][pos[j]] + mayores[j][pos[j]];
}
}
rep(i, 1, n){
dp[i] = INF;
repa(j, i, 1){
if (j == i) c[j][i] = mayores[j][pos[j]];
else c[j][i] = c[j][i - 1] + mov[j][i] - menoresderecha(i, pos[i]) + menoresderecha(j, pos[i]);
dp[i] = min(dp[i], dp[j - 1] + c[j][i]);
}
}
cout << dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
3 ms |
4428 KB |
Output is correct |
22 |
Correct |
4 ms |
4428 KB |
Output is correct |
23 |
Correct |
4 ms |
4428 KB |
Output is correct |
24 |
Correct |
4 ms |
4552 KB |
Output is correct |
25 |
Correct |
3 ms |
4428 KB |
Output is correct |
26 |
Correct |
4 ms |
4428 KB |
Output is correct |
27 |
Correct |
3 ms |
4428 KB |
Output is correct |
28 |
Correct |
3 ms |
4544 KB |
Output is correct |
29 |
Correct |
3 ms |
4428 KB |
Output is correct |
30 |
Correct |
3 ms |
4428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
3 ms |
4428 KB |
Output is correct |
22 |
Correct |
4 ms |
4428 KB |
Output is correct |
23 |
Correct |
4 ms |
4428 KB |
Output is correct |
24 |
Correct |
4 ms |
4552 KB |
Output is correct |
25 |
Correct |
3 ms |
4428 KB |
Output is correct |
26 |
Correct |
4 ms |
4428 KB |
Output is correct |
27 |
Correct |
3 ms |
4428 KB |
Output is correct |
28 |
Correct |
3 ms |
4544 KB |
Output is correct |
29 |
Correct |
3 ms |
4428 KB |
Output is correct |
30 |
Correct |
3 ms |
4428 KB |
Output is correct |
31 |
Correct |
43 ms |
28176 KB |
Output is correct |
32 |
Correct |
42 ms |
28216 KB |
Output is correct |
33 |
Correct |
37 ms |
28468 KB |
Output is correct |
34 |
Correct |
38 ms |
28416 KB |
Output is correct |
35 |
Correct |
36 ms |
28356 KB |
Output is correct |
36 |
Correct |
37 ms |
28360 KB |
Output is correct |
37 |
Correct |
35 ms |
28344 KB |
Output is correct |
38 |
Correct |
37 ms |
28352 KB |
Output is correct |
39 |
Correct |
37 ms |
28260 KB |
Output is correct |
40 |
Correct |
37 ms |
28368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
380 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
588 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
3 ms |
4428 KB |
Output is correct |
22 |
Correct |
4 ms |
4428 KB |
Output is correct |
23 |
Correct |
4 ms |
4428 KB |
Output is correct |
24 |
Correct |
4 ms |
4552 KB |
Output is correct |
25 |
Correct |
3 ms |
4428 KB |
Output is correct |
26 |
Correct |
4 ms |
4428 KB |
Output is correct |
27 |
Correct |
3 ms |
4428 KB |
Output is correct |
28 |
Correct |
3 ms |
4544 KB |
Output is correct |
29 |
Correct |
3 ms |
4428 KB |
Output is correct |
30 |
Correct |
3 ms |
4428 KB |
Output is correct |
31 |
Correct |
43 ms |
28176 KB |
Output is correct |
32 |
Correct |
42 ms |
28216 KB |
Output is correct |
33 |
Correct |
37 ms |
28468 KB |
Output is correct |
34 |
Correct |
38 ms |
28416 KB |
Output is correct |
35 |
Correct |
36 ms |
28356 KB |
Output is correct |
36 |
Correct |
37 ms |
28360 KB |
Output is correct |
37 |
Correct |
35 ms |
28344 KB |
Output is correct |
38 |
Correct |
37 ms |
28352 KB |
Output is correct |
39 |
Correct |
37 ms |
28260 KB |
Output is correct |
40 |
Correct |
37 ms |
28368 KB |
Output is correct |
41 |
Correct |
1831 ms |
625492 KB |
Output is correct |
42 |
Correct |
1850 ms |
625664 KB |
Output is correct |
43 |
Correct |
1875 ms |
625776 KB |
Output is correct |
44 |
Correct |
1849 ms |
625692 KB |
Output is correct |
45 |
Correct |
1845 ms |
625652 KB |
Output is correct |
46 |
Correct |
1834 ms |
625928 KB |
Output is correct |
47 |
Correct |
1837 ms |
625964 KB |
Output is correct |
48 |
Correct |
1878 ms |
625944 KB |
Output is correct |
49 |
Correct |
1872 ms |
625944 KB |
Output is correct |
50 |
Correct |
1874 ms |
626080 KB |
Output is correct |
51 |
Correct |
1852 ms |
626004 KB |
Output is correct |
52 |
Correct |
1953 ms |
626052 KB |
Output is correct |
53 |
Correct |
1834 ms |
626092 KB |
Output is correct |
54 |
Correct |
1860 ms |
626024 KB |
Output is correct |
55 |
Correct |
1850 ms |
625960 KB |
Output is correct |