#include <bits/stdc++.h>
//ako ovaj vaso daso misli da me pobedjuje.....
using namespace std;
#define int long long
#define f first
#define s second
const int N = 5010;
int dp[N], a[N], n, cost[N][N], fenw[N], pos[N];
//cost[i][j] -> prvih i je reseno i ja pocinjem od j
//prvi broj je i + 1
void add(int v, int val) {
for (int i = v;i < N;i += (i & -i))
fenw[i] += val;
}
int get(int v) {
int res = 0;
for (int i = v;i > 0;i -= (i & -i))
res += fenw[i];
return res;
}
void solve() {
for (int i = 1;i <= n;i++) {
int s = 0;
vector<int> p(n + 1);
for (int j = n;j >= 1;j--) {
if (a[j] < i)
s++;
else
p[a[j]] = pos[a[j]] + s;
}
for (int j = i;j <= n;j++) {
cost[i][j] = cost[i][j - 1] + p[j] - i - get(p[j]);
add(1, 1);
add(p[j] + 1, -1);
}
for (auto &i : fenw)
i = 0;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1;i <= n;i++) {
dp[i] = 1e9;
cin >> a[i];
pos[a[i]] = i;
}
solve();
for (int i = 1;i <= n;i++) {
for (int j = i - 1;j >= 0;j--)
dp[i] = min(dp[i], dp[j] + cost[j + 1][i]);
}
cout << dp[n];
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1272 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1272 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
13 ms |
6232 KB |
Output is correct |
32 |
Correct |
13 ms |
6228 KB |
Output is correct |
33 |
Correct |
14 ms |
6216 KB |
Output is correct |
34 |
Correct |
16 ms |
6236 KB |
Output is correct |
35 |
Correct |
14 ms |
6072 KB |
Output is correct |
36 |
Correct |
13 ms |
6148 KB |
Output is correct |
37 |
Correct |
13 ms |
6232 KB |
Output is correct |
38 |
Correct |
13 ms |
6232 KB |
Output is correct |
39 |
Correct |
13 ms |
6232 KB |
Output is correct |
40 |
Correct |
13 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
1372 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
2 ms |
1372 KB |
Output is correct |
27 |
Correct |
2 ms |
1372 KB |
Output is correct |
28 |
Correct |
3 ms |
1272 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
13 ms |
6232 KB |
Output is correct |
32 |
Correct |
13 ms |
6228 KB |
Output is correct |
33 |
Correct |
14 ms |
6216 KB |
Output is correct |
34 |
Correct |
16 ms |
6236 KB |
Output is correct |
35 |
Correct |
14 ms |
6072 KB |
Output is correct |
36 |
Correct |
13 ms |
6148 KB |
Output is correct |
37 |
Correct |
13 ms |
6232 KB |
Output is correct |
38 |
Correct |
13 ms |
6232 KB |
Output is correct |
39 |
Correct |
13 ms |
6232 KB |
Output is correct |
40 |
Correct |
13 ms |
6236 KB |
Output is correct |
41 |
Correct |
489 ms |
141048 KB |
Output is correct |
42 |
Correct |
489 ms |
141264 KB |
Output is correct |
43 |
Correct |
484 ms |
141404 KB |
Output is correct |
44 |
Correct |
486 ms |
141140 KB |
Output is correct |
45 |
Correct |
491 ms |
141208 KB |
Output is correct |
46 |
Correct |
501 ms |
141340 KB |
Output is correct |
47 |
Correct |
483 ms |
141340 KB |
Output is correct |
48 |
Correct |
489 ms |
141308 KB |
Output is correct |
49 |
Correct |
486 ms |
141544 KB |
Output is correct |
50 |
Correct |
484 ms |
141140 KB |
Output is correct |
51 |
Correct |
488 ms |
141340 KB |
Output is correct |
52 |
Correct |
497 ms |
141552 KB |
Output is correct |
53 |
Correct |
493 ms |
141296 KB |
Output is correct |
54 |
Correct |
490 ms |
141316 KB |
Output is correct |
55 |
Correct |
498 ms |
141280 KB |
Output is correct |