#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;
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;
}
}
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;
}
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();
cost.pop_back();
}
}
cout << dp[n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
440 KB |
Output is correct |
22 |
Correct |
4 ms |
352 KB |
Output is correct |
23 |
Correct |
4 ms |
356 KB |
Output is correct |
24 |
Correct |
4 ms |
444 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
5 ms |
344 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
440 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
440 KB |
Output is correct |
22 |
Correct |
4 ms |
352 KB |
Output is correct |
23 |
Correct |
4 ms |
356 KB |
Output is correct |
24 |
Correct |
4 ms |
444 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
5 ms |
344 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
440 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
247 ms |
444 KB |
Output is correct |
32 |
Correct |
257 ms |
596 KB |
Output is correct |
33 |
Correct |
232 ms |
344 KB |
Output is correct |
34 |
Correct |
236 ms |
348 KB |
Output is correct |
35 |
Correct |
234 ms |
344 KB |
Output is correct |
36 |
Correct |
257 ms |
344 KB |
Output is correct |
37 |
Correct |
242 ms |
428 KB |
Output is correct |
38 |
Correct |
249 ms |
428 KB |
Output is correct |
39 |
Correct |
283 ms |
600 KB |
Output is correct |
40 |
Correct |
237 ms |
432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 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 |
432 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
432 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 |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
440 KB |
Output is correct |
22 |
Correct |
4 ms |
352 KB |
Output is correct |
23 |
Correct |
4 ms |
356 KB |
Output is correct |
24 |
Correct |
4 ms |
444 KB |
Output is correct |
25 |
Correct |
4 ms |
348 KB |
Output is correct |
26 |
Correct |
4 ms |
348 KB |
Output is correct |
27 |
Correct |
5 ms |
344 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
4 ms |
440 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
247 ms |
444 KB |
Output is correct |
32 |
Correct |
257 ms |
596 KB |
Output is correct |
33 |
Correct |
232 ms |
344 KB |
Output is correct |
34 |
Correct |
236 ms |
348 KB |
Output is correct |
35 |
Correct |
234 ms |
344 KB |
Output is correct |
36 |
Correct |
257 ms |
344 KB |
Output is correct |
37 |
Correct |
242 ms |
428 KB |
Output is correct |
38 |
Correct |
249 ms |
428 KB |
Output is correct |
39 |
Correct |
283 ms |
600 KB |
Output is correct |
40 |
Correct |
237 ms |
432 KB |
Output is correct |
41 |
Execution timed out |
5064 ms |
348 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |