#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
int N;
cin >> N;
vector<int> arr(N + 1), pos(N + 1);
for (int i = 1; i <= N; i++) cin >> arr[i], pos[arr[i]] = i;
vector<vector<int>> dp(N + 1, vector<int>(N + 1, 0));
dp[1][1] = 0;
auto inv = [&](int up, int val, int p) -> int {
int cnt = 0;
for (int i = val; i <= up; i++) {
if (pos[i] < p) cnt++;
}
for (int i = 0; i < val; i++) {
if (pos[i] > p) cnt++;
}
return cnt;
};
for (int i = 2; i <= N; i++) {
dp[i][i] = 1e9;
for (int j = 1; j < i; j++) {
dp[i][j] = dp[i - 1][j] + inv(i - 1, j, pos[i]);
dp[i][i] = min(dp[i][i], dp[i - 1][j] + inv(i - 1, i, pos[i]));
}
for (int j = 1; j <= i; j++) {
// dbg(i, j, dp[i][j]);
}
}
int ans = 1e9;
for (int i = 1; i <= N; i++) ans = min(ans, dp[N][i]);
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 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 |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
604 KB |
Output is correct |
23 |
Correct |
4 ms |
604 KB |
Output is correct |
24 |
Correct |
4 ms |
604 KB |
Output is correct |
25 |
Correct |
4 ms |
624 KB |
Output is correct |
26 |
Correct |
5 ms |
600 KB |
Output is correct |
27 |
Correct |
7 ms |
600 KB |
Output is correct |
28 |
Correct |
4 ms |
604 KB |
Output is correct |
29 |
Correct |
4 ms |
456 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
604 KB |
Output is correct |
23 |
Correct |
4 ms |
604 KB |
Output is correct |
24 |
Correct |
4 ms |
604 KB |
Output is correct |
25 |
Correct |
4 ms |
624 KB |
Output is correct |
26 |
Correct |
5 ms |
600 KB |
Output is correct |
27 |
Correct |
7 ms |
600 KB |
Output is correct |
28 |
Correct |
4 ms |
604 KB |
Output is correct |
29 |
Correct |
4 ms |
456 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
217 ms |
2980 KB |
Output is correct |
32 |
Correct |
236 ms |
2976 KB |
Output is correct |
33 |
Correct |
213 ms |
2984 KB |
Output is correct |
34 |
Correct |
251 ms |
2988 KB |
Output is correct |
35 |
Correct |
219 ms |
2980 KB |
Output is correct |
36 |
Correct |
219 ms |
2988 KB |
Output is correct |
37 |
Correct |
217 ms |
2980 KB |
Output is correct |
38 |
Correct |
226 ms |
2996 KB |
Output is correct |
39 |
Correct |
215 ms |
2980 KB |
Output is correct |
40 |
Correct |
221 ms |
2984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
604 KB |
Output is correct |
23 |
Correct |
4 ms |
604 KB |
Output is correct |
24 |
Correct |
4 ms |
604 KB |
Output is correct |
25 |
Correct |
4 ms |
624 KB |
Output is correct |
26 |
Correct |
5 ms |
600 KB |
Output is correct |
27 |
Correct |
7 ms |
600 KB |
Output is correct |
28 |
Correct |
4 ms |
604 KB |
Output is correct |
29 |
Correct |
4 ms |
456 KB |
Output is correct |
30 |
Correct |
4 ms |
604 KB |
Output is correct |
31 |
Correct |
217 ms |
2980 KB |
Output is correct |
32 |
Correct |
236 ms |
2976 KB |
Output is correct |
33 |
Correct |
213 ms |
2984 KB |
Output is correct |
34 |
Correct |
251 ms |
2988 KB |
Output is correct |
35 |
Correct |
219 ms |
2980 KB |
Output is correct |
36 |
Correct |
219 ms |
2988 KB |
Output is correct |
37 |
Correct |
217 ms |
2980 KB |
Output is correct |
38 |
Correct |
226 ms |
2996 KB |
Output is correct |
39 |
Correct |
215 ms |
2980 KB |
Output is correct |
40 |
Correct |
221 ms |
2984 KB |
Output is correct |
41 |
Execution timed out |
5020 ms |
98192 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |