#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
using namespace std;
int n;
int const inf = 1e9;
vector<int>a, idx;
vector<int>cost;
int total_cost;
vector<vector<int>>left_num, right_num;
void precalc_cost(){
left_num.resize(n+1);
right_num.resize(n+1);
for(int i = 1; i <= n; i++){
for(int j = i+1; j <=n; j++){
left_num[a[j]].push_back(a[i]);
}
}
for(int i = n; i >= 1; i--){
for(int j = i-1; j >= 1; j--){
right_num[a[j]].push_back(a[i]);
}
}
for(int i = 1; i <= n; i++){
sort(left_num[i].begin(), left_num[i].end());
sort(right_num[i].begin(), right_num[i].end());
}
}
int query(vector<int> &v, int l, int r){
if(l > r)
return 0;
//numbers less than or equal to r
int num_r = lower_bound(v.begin(), v.end(), r+1)-v.begin();
//numbers less than l
int num_l = lower_bound(v.begin(), v.end(),l)-v.begin();
return num_r-num_l;
}
int query_left(int num, int l, int r){
return query(left_num[num], l, r);
}
int query_right(int num, int l, int r){
return query(right_num[num], l, r);
}
void calculate_cost(int val){
vector<int>p(n+1);
// iota(p.begin(), p.end(), 0);
// reverse(p.begin()+1, p.begin()+val+1);
// for(int i = val; i >= 1; i--){
// int inv = 0;
// for(int j = i+1; j <= n; j++){
// if((p[i] < p[j] && idx[i] > idx[j]) ||
// (p[i] > p[j] && idx[i] < idx[j]))
// inv++;
// }
// cost.push_back(inv);
// total_cost += inv;
// }
iota(p.begin(), p.end(), 0);
reverse(p.begin()+1, p.begin()+val+1);
int inv = 0;
for(int i = val; i >= 1; i--){
inv += query_left(i, val+1, n);
inv += query_right(i, i+1, val);
cost.push_back(inv);
total_cost += inv;
}
}
int main(){
cin >> n;
a.resize(n+1);
idx.resize(n+1);
vector<int>dp(n+1, inf);
for(int i = 1; i <= n; i++){
cin >> a[i];
idx[a[i]] = i;
}
precalc_cost();
dp[0] = 0;
for(int i = 1; i <= n; i++){
calculate_cost(i);
for(int j = 1; j <= i; j++){
// dp[i] = min(dp[i], dp[j-1] + total_cost);
// total_cost -= cost.back();
dp[i] = min(dp[i], dp[j-1] + cost.back());
cost.pop_back();
}
}
cout << dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
4 ms |
608 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
708 KB |
Output is correct |
25 |
Correct |
4 ms |
608 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
704 KB |
Output is correct |
28 |
Correct |
3 ms |
608 KB |
Output is correct |
29 |
Correct |
3 ms |
608 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
4 ms |
608 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
708 KB |
Output is correct |
25 |
Correct |
4 ms |
608 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
704 KB |
Output is correct |
28 |
Correct |
3 ms |
608 KB |
Output is correct |
29 |
Correct |
3 ms |
608 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
60 ms |
4188 KB |
Output is correct |
32 |
Correct |
62 ms |
4188 KB |
Output is correct |
33 |
Correct |
63 ms |
4192 KB |
Output is correct |
34 |
Correct |
60 ms |
4184 KB |
Output is correct |
35 |
Correct |
63 ms |
4184 KB |
Output is correct |
36 |
Correct |
60 ms |
4188 KB |
Output is correct |
37 |
Correct |
61 ms |
4188 KB |
Output is correct |
38 |
Correct |
59 ms |
4184 KB |
Output is correct |
39 |
Correct |
61 ms |
4184 KB |
Output is correct |
40 |
Correct |
63 ms |
4192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
352 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
4 ms |
608 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
3 ms |
708 KB |
Output is correct |
25 |
Correct |
4 ms |
608 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
704 KB |
Output is correct |
28 |
Correct |
3 ms |
608 KB |
Output is correct |
29 |
Correct |
3 ms |
608 KB |
Output is correct |
30 |
Correct |
3 ms |
604 KB |
Output is correct |
31 |
Correct |
60 ms |
4188 KB |
Output is correct |
32 |
Correct |
62 ms |
4188 KB |
Output is correct |
33 |
Correct |
63 ms |
4192 KB |
Output is correct |
34 |
Correct |
60 ms |
4184 KB |
Output is correct |
35 |
Correct |
63 ms |
4184 KB |
Output is correct |
36 |
Correct |
60 ms |
4188 KB |
Output is correct |
37 |
Correct |
61 ms |
4188 KB |
Output is correct |
38 |
Correct |
59 ms |
4184 KB |
Output is correct |
39 |
Correct |
61 ms |
4184 KB |
Output is correct |
40 |
Correct |
63 ms |
4192 KB |
Output is correct |
41 |
Correct |
3449 ms |
135160 KB |
Output is correct |
42 |
Correct |
3568 ms |
135208 KB |
Output is correct |
43 |
Correct |
3474 ms |
134992 KB |
Output is correct |
44 |
Correct |
3568 ms |
134992 KB |
Output is correct |
45 |
Correct |
3533 ms |
135248 KB |
Output is correct |
46 |
Correct |
3459 ms |
135272 KB |
Output is correct |
47 |
Correct |
3511 ms |
135276 KB |
Output is correct |
48 |
Correct |
3565 ms |
135252 KB |
Output is correct |
49 |
Correct |
3525 ms |
135280 KB |
Output is correct |
50 |
Correct |
3536 ms |
135360 KB |
Output is correct |
51 |
Correct |
3469 ms |
135028 KB |
Output is correct |
52 |
Correct |
3623 ms |
135248 KB |
Output is correct |
53 |
Correct |
3503 ms |
135248 KB |
Output is correct |
54 |
Correct |
3430 ms |
135248 KB |
Output is correct |
55 |
Correct |
3561 ms |
135280 KB |
Output is correct |