#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));
vector<vector<int>> psum(N + 1, vector<int>(N + 1, 0));
dp[1][1] = 0;
psum[1][pos[1]] = 1;
for (int i = 1; i <= N; i++) psum[1][i] += psum[1][i - 1];
auto inv = [&](int up, int val, int p) -> int {
// how much >= val and position < p
int cnt = 0, cnt2 = 0;
cnt += psum[up][p] - psum[val - 1][p];
// for (int i = val; i <= up; i++) {
// if (pos[i] < p) cnt2++;
// }
// dbg(up, val, cnt, cnt2);
cnt += psum[val - 1][N] - psum[val - 1][p];
// for (int i = 0; i < val; i++) {
// if (pos[i] > p) cnt2++;
// }
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++) {
psum[i][pos[j]]++;
}
for (int j = 1; j <= N; j++) {
psum[i][j] += psum[i][j - 1];
// dbg(i, j, psum[i][j]);
}
}
int ans = 1e9;
for (int i = 1; i <= N; i++) ans = min(ans, dp[N][i]);
cout << ans << '\n';
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:27:22: warning: unused variable 'cnt2' [-Wunused-variable]
27 | int cnt = 0, cnt2 = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
6 ms |
5464 KB |
Output is correct |
32 |
Correct |
5 ms |
5464 KB |
Output is correct |
33 |
Correct |
5 ms |
5464 KB |
Output is correct |
34 |
Correct |
5 ms |
5468 KB |
Output is correct |
35 |
Correct |
5 ms |
5468 KB |
Output is correct |
36 |
Correct |
5 ms |
5552 KB |
Output is correct |
37 |
Correct |
5 ms |
5468 KB |
Output is correct |
38 |
Correct |
5 ms |
5468 KB |
Output is correct |
39 |
Correct |
5 ms |
5432 KB |
Output is correct |
40 |
Correct |
5 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
6 ms |
5464 KB |
Output is correct |
32 |
Correct |
5 ms |
5464 KB |
Output is correct |
33 |
Correct |
5 ms |
5464 KB |
Output is correct |
34 |
Correct |
5 ms |
5468 KB |
Output is correct |
35 |
Correct |
5 ms |
5468 KB |
Output is correct |
36 |
Correct |
5 ms |
5552 KB |
Output is correct |
37 |
Correct |
5 ms |
5468 KB |
Output is correct |
38 |
Correct |
5 ms |
5468 KB |
Output is correct |
39 |
Correct |
5 ms |
5432 KB |
Output is correct |
40 |
Correct |
5 ms |
5468 KB |
Output is correct |
41 |
Correct |
476 ms |
196348 KB |
Output is correct |
42 |
Correct |
430 ms |
196560 KB |
Output is correct |
43 |
Correct |
440 ms |
196700 KB |
Output is correct |
44 |
Correct |
447 ms |
196600 KB |
Output is correct |
45 |
Correct |
439 ms |
196692 KB |
Output is correct |
46 |
Correct |
421 ms |
196644 KB |
Output is correct |
47 |
Correct |
434 ms |
196436 KB |
Output is correct |
48 |
Correct |
472 ms |
196640 KB |
Output is correct |
49 |
Correct |
436 ms |
196524 KB |
Output is correct |
50 |
Correct |
442 ms |
196692 KB |
Output is correct |
51 |
Correct |
423 ms |
196636 KB |
Output is correct |
52 |
Correct |
417 ms |
196432 KB |
Output is correct |
53 |
Correct |
452 ms |
196640 KB |
Output is correct |
54 |
Correct |
423 ms |
196636 KB |
Output is correct |
55 |
Correct |
469 ms |
196692 KB |
Output is correct |