// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 1e9 + 7;
ll binpow(ll a, ll p, int mod = MOD) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
const int N = 5005;
int dist[N][N];
int nxt[N][N], prv[N][N];
int dp[N];
int a[N], pos[N];
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (pos[j] > pos[i]) {
nxt[i][j] = 1;
}
if (pos[j] < pos[i]) {
prv[i][j] = 1;
}
}
for (int j = 1; j <= n; j++) {
nxt[i][j] += nxt[i][j - 1];
prv[i][j] += prv[i][j - 1];
}
}
for (int i = 1; i <= n; i++) {
dist[i][i] = prv[i][n] - prv[i][i];
}
for (int len = 2; len <= n; len++) {
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
dist[i][j] = dist[i][j - 1] + (prv[j][n] - prv[j][i - 1]) - (nxt[j][j - 1] - nxt[j][i - 1]);
}
}
for (int i = 1; i <= n; i++) {
dp[i] = 1e9;
for (int j = 0; j < i; j++) {
dp[i] = min(dp[i], dp[j] + dist[j + 1][i]);
}
}
cout << dp[n] << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
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 |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 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 |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 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 |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
3020 KB |
Output is correct |
22 |
Correct |
4 ms |
3136 KB |
Output is correct |
23 |
Correct |
4 ms |
3148 KB |
Output is correct |
24 |
Correct |
3 ms |
3148 KB |
Output is correct |
25 |
Correct |
3 ms |
3136 KB |
Output is correct |
26 |
Correct |
3 ms |
3148 KB |
Output is correct |
27 |
Correct |
3 ms |
3148 KB |
Output is correct |
28 |
Correct |
5 ms |
3148 KB |
Output is correct |
29 |
Correct |
3 ms |
3148 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 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 |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
3020 KB |
Output is correct |
22 |
Correct |
4 ms |
3136 KB |
Output is correct |
23 |
Correct |
4 ms |
3148 KB |
Output is correct |
24 |
Correct |
3 ms |
3148 KB |
Output is correct |
25 |
Correct |
3 ms |
3136 KB |
Output is correct |
26 |
Correct |
3 ms |
3148 KB |
Output is correct |
27 |
Correct |
3 ms |
3148 KB |
Output is correct |
28 |
Correct |
5 ms |
3148 KB |
Output is correct |
29 |
Correct |
3 ms |
3148 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 KB |
Output is correct |
31 |
Correct |
20 ms |
16204 KB |
Output is correct |
32 |
Correct |
19 ms |
16204 KB |
Output is correct |
33 |
Correct |
21 ms |
16260 KB |
Output is correct |
34 |
Correct |
24 ms |
16280 KB |
Output is correct |
35 |
Correct |
23 ms |
16232 KB |
Output is correct |
36 |
Correct |
21 ms |
16196 KB |
Output is correct |
37 |
Correct |
25 ms |
16256 KB |
Output is correct |
38 |
Correct |
27 ms |
16208 KB |
Output is correct |
39 |
Correct |
26 ms |
16176 KB |
Output is correct |
40 |
Correct |
21 ms |
16176 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 |
332 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
3020 KB |
Output is correct |
22 |
Correct |
4 ms |
3136 KB |
Output is correct |
23 |
Correct |
4 ms |
3148 KB |
Output is correct |
24 |
Correct |
3 ms |
3148 KB |
Output is correct |
25 |
Correct |
3 ms |
3136 KB |
Output is correct |
26 |
Correct |
3 ms |
3148 KB |
Output is correct |
27 |
Correct |
3 ms |
3148 KB |
Output is correct |
28 |
Correct |
5 ms |
3148 KB |
Output is correct |
29 |
Correct |
3 ms |
3148 KB |
Output is correct |
30 |
Correct |
2 ms |
3148 KB |
Output is correct |
31 |
Correct |
20 ms |
16204 KB |
Output is correct |
32 |
Correct |
19 ms |
16204 KB |
Output is correct |
33 |
Correct |
21 ms |
16260 KB |
Output is correct |
34 |
Correct |
24 ms |
16280 KB |
Output is correct |
35 |
Correct |
23 ms |
16232 KB |
Output is correct |
36 |
Correct |
21 ms |
16196 KB |
Output is correct |
37 |
Correct |
25 ms |
16256 KB |
Output is correct |
38 |
Correct |
27 ms |
16208 KB |
Output is correct |
39 |
Correct |
26 ms |
16176 KB |
Output is correct |
40 |
Correct |
21 ms |
16176 KB |
Output is correct |
41 |
Correct |
971 ms |
263084 KB |
Output is correct |
42 |
Correct |
987 ms |
263108 KB |
Output is correct |
43 |
Correct |
1010 ms |
263108 KB |
Output is correct |
44 |
Correct |
955 ms |
263204 KB |
Output is correct |
45 |
Correct |
1006 ms |
263100 KB |
Output is correct |
46 |
Correct |
984 ms |
263280 KB |
Output is correct |
47 |
Correct |
955 ms |
263240 KB |
Output is correct |
48 |
Correct |
974 ms |
263260 KB |
Output is correct |
49 |
Correct |
1024 ms |
263260 KB |
Output is correct |
50 |
Correct |
942 ms |
263188 KB |
Output is correct |
51 |
Correct |
999 ms |
263380 KB |
Output is correct |
52 |
Correct |
1031 ms |
263188 KB |
Output is correct |
53 |
Correct |
917 ms |
263212 KB |
Output is correct |
54 |
Correct |
924 ms |
263176 KB |
Output is correct |
55 |
Correct |
942 ms |
263348 KB |
Output is correct |