#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define x first
#define y second
#define elif else if
#define endl '\n'
#define all(x) begin(x), end(x)
int n,a[5069],pos[5069];
bool done[5069];
int cnt[5069][5069];
int dp[5069][5069];
int minimum=INT_MAX;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
for (int i = 0; i < n; i++) {
cin>>a[i];
a[i]--;
pos[a[i]]=i;
cnt[i][0]=i-done[0];
for (int j = 1; j < n; j++) {
cnt[i][j]=cnt[i][j-1]-done[j];
}
done[a[i]]=true;
}
for (int i = n-1; i >= 0; i--) {
dp[i][i]=INT_MAX;
for (int j = i+1; j < n; j++) {
dp[i][i]=min(dp[i][i],dp[i+1][j]);
dp[i][j]=dp[i+1][j]+cnt[pos[i]][j]+j-i-(cnt[pos[i]][i]-cnt[pos[i]][j]);
}
if (dp[i][i]==INT_MAX) {
dp[i][i]=0;
}
dp[i][i]+=cnt[pos[i]][i];
}
for (int i = 0; i < n; i++) {
minimum=min(minimum,dp[0][i]);
}
cout<<minimum<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2156 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
2 ms |
2156 KB |
Output is correct |
25 |
Correct |
2 ms |
2156 KB |
Output is correct |
26 |
Correct |
3 ms |
2156 KB |
Output is correct |
27 |
Correct |
3 ms |
2156 KB |
Output is correct |
28 |
Correct |
2 ms |
2156 KB |
Output is correct |
29 |
Correct |
2 ms |
2156 KB |
Output is correct |
30 |
Correct |
2 ms |
2156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2156 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
2 ms |
2156 KB |
Output is correct |
25 |
Correct |
2 ms |
2156 KB |
Output is correct |
26 |
Correct |
3 ms |
2156 KB |
Output is correct |
27 |
Correct |
3 ms |
2156 KB |
Output is correct |
28 |
Correct |
2 ms |
2156 KB |
Output is correct |
29 |
Correct |
2 ms |
2156 KB |
Output is correct |
30 |
Correct |
2 ms |
2156 KB |
Output is correct |
31 |
Correct |
8 ms |
10604 KB |
Output is correct |
32 |
Correct |
8 ms |
10604 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
34 |
Correct |
8 ms |
10604 KB |
Output is correct |
35 |
Correct |
8 ms |
10604 KB |
Output is correct |
36 |
Correct |
8 ms |
10604 KB |
Output is correct |
37 |
Correct |
9 ms |
10604 KB |
Output is correct |
38 |
Correct |
8 ms |
10604 KB |
Output is correct |
39 |
Correct |
8 ms |
10604 KB |
Output is correct |
40 |
Correct |
9 ms |
10604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
2 ms |
2156 KB |
Output is correct |
22 |
Correct |
2 ms |
2156 KB |
Output is correct |
23 |
Correct |
2 ms |
2156 KB |
Output is correct |
24 |
Correct |
2 ms |
2156 KB |
Output is correct |
25 |
Correct |
2 ms |
2156 KB |
Output is correct |
26 |
Correct |
3 ms |
2156 KB |
Output is correct |
27 |
Correct |
3 ms |
2156 KB |
Output is correct |
28 |
Correct |
2 ms |
2156 KB |
Output is correct |
29 |
Correct |
2 ms |
2156 KB |
Output is correct |
30 |
Correct |
2 ms |
2156 KB |
Output is correct |
31 |
Correct |
8 ms |
10604 KB |
Output is correct |
32 |
Correct |
8 ms |
10604 KB |
Output is correct |
33 |
Correct |
8 ms |
10604 KB |
Output is correct |
34 |
Correct |
8 ms |
10604 KB |
Output is correct |
35 |
Correct |
8 ms |
10604 KB |
Output is correct |
36 |
Correct |
8 ms |
10604 KB |
Output is correct |
37 |
Correct |
9 ms |
10604 KB |
Output is correct |
38 |
Correct |
8 ms |
10604 KB |
Output is correct |
39 |
Correct |
8 ms |
10604 KB |
Output is correct |
40 |
Correct |
9 ms |
10604 KB |
Output is correct |
41 |
Correct |
162 ms |
166764 KB |
Output is correct |
42 |
Correct |
160 ms |
166764 KB |
Output is correct |
43 |
Correct |
165 ms |
166764 KB |
Output is correct |
44 |
Correct |
163 ms |
166764 KB |
Output is correct |
45 |
Correct |
163 ms |
166764 KB |
Output is correct |
46 |
Correct |
164 ms |
166892 KB |
Output is correct |
47 |
Correct |
163 ms |
166892 KB |
Output is correct |
48 |
Correct |
162 ms |
166892 KB |
Output is correct |
49 |
Correct |
169 ms |
166892 KB |
Output is correct |
50 |
Correct |
161 ms |
166892 KB |
Output is correct |
51 |
Correct |
164 ms |
166892 KB |
Output is correct |
52 |
Correct |
160 ms |
166892 KB |
Output is correct |
53 |
Correct |
160 ms |
166892 KB |
Output is correct |
54 |
Correct |
169 ms |
166780 KB |
Output is correct |
55 |
Correct |
160 ms |
166892 KB |
Output is correct |