#include <bits/stdc++.h>
using namespace std;
int parent[1100100];
int v[1100100];
bool done[1100100];
int l[1100100];
int r[1100100];
pair<int,int> expand_range(int l2, int r2, int h){
int l1 = min(l2,h);
int r1 = max(r2,h);
while(l1 != l2 || r1 != r2){
if(l1 != l2){
l2--;
l1 = min(l1,l[parent[l2]]);
r1 = max(r1,r[parent[l2]]);
}
else{
r2++;
l1 = min(l1,l[parent[r2]]);
r1 = max(r1,r[parent[r2]]);
}
}
return {l2,r2};
}
long long minimum_walk(vector<int> p, int s) {
int l1 = s;
int r1 = s;
int l2 = s;
int r2 = s;
long long int some = 0;
int N = p.size();
for(int i = 0; i < N; i++){
done[i] = false;
}
for(int i = 0; i < N; i++){
if(done[i]) continue;
l[i] = i;
r[i] = i;
parent[i] = i;
int j = p[i];
some += abs(i - j);
while(j != i){
int temp = j;
parent[j] = i;
done[j] = true;
r[i] = max(r[i],j);
j = p[j];
some += abs(temp - j);
}
}
l2 = l[parent[s]];
r2 = r[parent[s]];
while(l1 != l2 || r1 != r2){
if(l1 != l2){
l1--;
l2 = min(l2,l[parent[l1]]);
r2 = max(r2,r[parent[l1]]);
}
else{
r1++;
l2 = min(l2,l[parent[r1]]);
r2 = max(r2,r[parent[r1]]);
}
}
int l3l = l1;
int r3l = r1;
int l3r = l1;
int r3r = r1;
while(l1 != 0 || r1 != N - 1){
l3l = l1;
r3l = r1;
l3r = l1;
r3r = r1;
int tcoft = 0;
int coft = 0;
while(!(r1 < r3l || l3r < l1)){
coft += 2;
tcoft += 2;
if(l3l > 0){
pair<int,int> ii = expand_range(l3l,r3l,l3l - 1);
l3l = ii.first;
r3l = ii.second;
}
else{
tcoft -= 2;
}
if(r3r < N - 1){
pair<int,int> ii = expand_range(l3r,r3r,r3r+1);
l3r = ii.first;
r3r = ii.second;
}
else{
tcoft -=2;
}
if(!(r1 < r3l || l3r < l1) && l3l == 0 && r3r == N - 1){
some += tcoft;
break;
}
}
some += coft;
l1 = min(l3l,l1);
l1 = min(l3r,l1);
r1 = max(r3l,r1);
r1 = max(r3r,r1);
}
//printf("%d",some);
for(int i = N - 1; i > s; i--){
if(p[i] != i) break;
some -= 2;
}
for(int i = 0; i < s; i++){
if(p[i] != i) break;
some -= 2;
}
return some;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6596 KB |
Output is correct |
4 |
Correct |
1 ms |
6500 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6592 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6596 KB |
Output is correct |
4 |
Correct |
1 ms |
6500 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6592 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6588 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6588 KB |
Output is correct |
28 |
Correct |
1 ms |
6588 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6596 KB |
Output is correct |
4 |
Correct |
1 ms |
6500 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6592 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6588 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6588 KB |
Output is correct |
28 |
Correct |
1 ms |
6588 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
95 ms |
23288 KB |
Output is correct |
31 |
Correct |
91 ms |
21240 KB |
Output is correct |
32 |
Correct |
80 ms |
27472 KB |
Output is correct |
33 |
Correct |
84 ms |
27472 KB |
Output is correct |
34 |
Correct |
93 ms |
27640 KB |
Output is correct |
35 |
Correct |
99 ms |
27620 KB |
Output is correct |
36 |
Correct |
81 ms |
27476 KB |
Output is correct |
37 |
Correct |
83 ms |
27624 KB |
Output is correct |
38 |
Correct |
79 ms |
27600 KB |
Output is correct |
39 |
Correct |
79 ms |
27640 KB |
Output is correct |
40 |
Correct |
90 ms |
27472 KB |
Output is correct |
41 |
Correct |
91 ms |
27472 KB |
Output is correct |
42 |
Correct |
91 ms |
27456 KB |
Output is correct |
43 |
Correct |
110 ms |
27472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6600 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6588 KB |
Output is correct |
7 |
Correct |
1 ms |
6584 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6596 KB |
Output is correct |
4 |
Correct |
1 ms |
6500 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6744 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6488 KB |
Output is correct |
16 |
Correct |
1 ms |
6592 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6588 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6588 KB |
Output is correct |
28 |
Correct |
1 ms |
6588 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
95 ms |
23288 KB |
Output is correct |
31 |
Correct |
91 ms |
21240 KB |
Output is correct |
32 |
Correct |
80 ms |
27472 KB |
Output is correct |
33 |
Correct |
84 ms |
27472 KB |
Output is correct |
34 |
Correct |
93 ms |
27640 KB |
Output is correct |
35 |
Correct |
99 ms |
27620 KB |
Output is correct |
36 |
Correct |
81 ms |
27476 KB |
Output is correct |
37 |
Correct |
83 ms |
27624 KB |
Output is correct |
38 |
Correct |
79 ms |
27600 KB |
Output is correct |
39 |
Correct |
79 ms |
27640 KB |
Output is correct |
40 |
Correct |
90 ms |
27472 KB |
Output is correct |
41 |
Correct |
91 ms |
27472 KB |
Output is correct |
42 |
Correct |
91 ms |
27456 KB |
Output is correct |
43 |
Correct |
110 ms |
27472 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6600 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
1 ms |
6492 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
1 ms |
6588 KB |
Output is correct |
50 |
Correct |
1 ms |
6584 KB |
Output is correct |
51 |
Correct |
1 ms |
6492 KB |
Output is correct |
52 |
Correct |
1 ms |
6492 KB |
Output is correct |
53 |
Correct |
1 ms |
6492 KB |
Output is correct |
54 |
Correct |
1 ms |
6492 KB |
Output is correct |
55 |
Correct |
2 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
58 |
Correct |
1 ms |
6492 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
1 ms |
6492 KB |
Output is correct |
62 |
Correct |
1 ms |
6492 KB |
Output is correct |
63 |
Correct |
1 ms |
6492 KB |
Output is correct |
64 |
Correct |
83 ms |
27604 KB |
Output is correct |
65 |
Correct |
80 ms |
27472 KB |
Output is correct |
66 |
Correct |
81 ms |
27640 KB |
Output is correct |
67 |
Correct |
84 ms |
27700 KB |
Output is correct |
68 |
Correct |
11 ms |
10072 KB |
Output is correct |
69 |
Correct |
9 ms |
10320 KB |
Output is correct |
70 |
Correct |
9 ms |
10076 KB |
Output is correct |
71 |
Correct |
9 ms |
9888 KB |
Output is correct |
72 |
Correct |
9 ms |
10076 KB |
Output is correct |
73 |
Correct |
10 ms |
9928 KB |
Output is correct |
74 |
Correct |
85 ms |
27488 KB |
Output is correct |
75 |
Correct |
99 ms |
27800 KB |
Output is correct |
76 |
Correct |
84 ms |
27636 KB |
Output is correct |
77 |
Correct |
81 ms |
27692 KB |
Output is correct |
78 |
Correct |
79 ms |
27448 KB |
Output is correct |
79 |
Correct |
86 ms |
27700 KB |
Output is correct |
80 |
Correct |
80 ms |
27476 KB |
Output is correct |
81 |
Correct |
76 ms |
27476 KB |
Output is correct |
82 |
Correct |
76 ms |
27476 KB |
Output is correct |
83 |
Correct |
80 ms |
27612 KB |
Output is correct |
84 |
Correct |
81 ms |
27636 KB |
Output is correct |
85 |
Correct |
80 ms |
27640 KB |
Output is correct |
86 |
Correct |
79 ms |
27636 KB |
Output is correct |
87 |
Correct |
103 ms |
27684 KB |
Output is correct |
88 |
Correct |
127 ms |
27924 KB |
Output is correct |
89 |
Correct |
80 ms |
27636 KB |
Output is correct |
90 |
Correct |
97 ms |
27556 KB |
Output is correct |
91 |
Correct |
85 ms |
27480 KB |
Output is correct |
92 |
Correct |
80 ms |
27576 KB |
Output is correct |