#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long minimum_walk(vector<int> p, int s){
int n = (int)p.size();
ll ans = 0;
vector<int> cycle(n, -1);
vector<int> cmi, cma;
int numCycles = 0;
for(int i = 0; i < n; i++){
if(cycle[i] == -1 && p[i] != i){
int mi = i, ma = i;
int j = i;
do
{
mi = min(mi, j);
ma = max(ma, j);
cycle[j] = numCycles;
ans += abs(j-p[j]);
j = p[j];
}while(j != i);
numCycles++;
cmi.push_back(mi);
cma.push_back(ma);
}
}
int mi = s, ma = s;
int l = s, r = s;
vector<bool> vis(numCycles);
bool over = true;
while(true){
int i = -1;
while(r <= ma){
if(cycle[r] != -1 && !vis[cycle[r]]){
i = r;
break;
}
else r++;
}
if(i == -1)
while(l >= mi){
if(cycle[l] != -1 && !vis[cycle[l]]){
i = l;
break;
}
else l--;
}
if(i == -1 && over){
int costL = 0, costR = 0;
int candL = -1, candR = -1;
int until = mi;
for(int j = l; j >= 0; j--){
if(j < until) costL += 2;
if(cycle[j] != -1){
until = min(until, cmi[cycle[j]]);
if(cma[cycle[j]] > ma){
candL = j;
break;
}
}
}
until = ma;
for(int j = r; j < n; j++){
if(j > until) costR += 2;
if(cycle[j] != -1){
until = max(until, cma[cycle[j]]);
if(cmi[cycle[j]] < mi){
candR = j;
break;
}
}
}
if(costL < costR) i = candL;
else i = candR;
if(i != -1) ans += min(costL, costR);
if(i == -1) over = false;
}
if(i == -1){
int oldR = r, oldL = l;
while(r < n){
if(cycle[r] != -1 && !vis[cycle[r]]){
i = r;
ans += 2*(r+1-oldR);
break;
}
r++;
}
if(i == -1)
while(l >= 0){
if(cycle[l] != -1 && !vis[cycle[l]]){
i = l;
ans += 2*(oldL+1-l);
break;
}
l--;
}
}
if(i == -1) break;
vis[cycle[i]] = true;
mi = min(mi, cmi[cycle[i]]);
ma = max(ma, cma[cycle[i]]);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
148 ms |
12036 KB |
Output is correct |
31 |
Correct |
148 ms |
17784 KB |
Output is correct |
32 |
Correct |
98 ms |
17748 KB |
Output is correct |
33 |
Correct |
107 ms |
19464 KB |
Output is correct |
34 |
Correct |
109 ms |
20500 KB |
Output is correct |
35 |
Correct |
112 ms |
20684 KB |
Output is correct |
36 |
Correct |
113 ms |
20516 KB |
Output is correct |
37 |
Correct |
101 ms |
19036 KB |
Output is correct |
38 |
Correct |
117 ms |
18780 KB |
Output is correct |
39 |
Correct |
99 ms |
18784 KB |
Output is correct |
40 |
Correct |
95 ms |
18764 KB |
Output is correct |
41 |
Correct |
115 ms |
18760 KB |
Output is correct |
42 |
Correct |
131 ms |
18640 KB |
Output is correct |
43 |
Correct |
108 ms |
20892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
3 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
148 ms |
12036 KB |
Output is correct |
31 |
Correct |
148 ms |
17784 KB |
Output is correct |
32 |
Correct |
98 ms |
17748 KB |
Output is correct |
33 |
Correct |
107 ms |
19464 KB |
Output is correct |
34 |
Correct |
109 ms |
20500 KB |
Output is correct |
35 |
Correct |
112 ms |
20684 KB |
Output is correct |
36 |
Correct |
113 ms |
20516 KB |
Output is correct |
37 |
Correct |
101 ms |
19036 KB |
Output is correct |
38 |
Correct |
117 ms |
18780 KB |
Output is correct |
39 |
Correct |
99 ms |
18784 KB |
Output is correct |
40 |
Correct |
95 ms |
18764 KB |
Output is correct |
41 |
Correct |
115 ms |
18760 KB |
Output is correct |
42 |
Correct |
131 ms |
18640 KB |
Output is correct |
43 |
Correct |
108 ms |
20892 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
300 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
300 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
3 ms |
300 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
296 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
98 ms |
19796 KB |
Output is correct |
65 |
Correct |
118 ms |
19688 KB |
Output is correct |
66 |
Correct |
102 ms |
19040 KB |
Output is correct |
67 |
Correct |
94 ms |
18920 KB |
Output is correct |
68 |
Correct |
10 ms |
2176 KB |
Output is correct |
69 |
Correct |
10 ms |
2044 KB |
Output is correct |
70 |
Correct |
15 ms |
2220 KB |
Output is correct |
71 |
Correct |
12 ms |
2228 KB |
Output is correct |
72 |
Correct |
15 ms |
2220 KB |
Output is correct |
73 |
Correct |
10 ms |
2048 KB |
Output is correct |
74 |
Correct |
103 ms |
20444 KB |
Output is correct |
75 |
Correct |
100 ms |
20444 KB |
Output is correct |
76 |
Correct |
104 ms |
20888 KB |
Output is correct |
77 |
Correct |
105 ms |
20916 KB |
Output is correct |
78 |
Correct |
113 ms |
22072 KB |
Output is correct |
79 |
Correct |
112 ms |
22080 KB |
Output is correct |
80 |
Correct |
90 ms |
18660 KB |
Output is correct |
81 |
Correct |
117 ms |
22852 KB |
Output is correct |
82 |
Correct |
104 ms |
22924 KB |
Output is correct |
83 |
Correct |
106 ms |
20816 KB |
Output is correct |
84 |
Correct |
101 ms |
20560 KB |
Output is correct |
85 |
Correct |
98 ms |
19800 KB |
Output is correct |
86 |
Correct |
94 ms |
19332 KB |
Output is correct |
87 |
Correct |
96 ms |
20440 KB |
Output is correct |
88 |
Correct |
107 ms |
22188 KB |
Output is correct |
89 |
Correct |
107 ms |
22088 KB |
Output is correct |
90 |
Correct |
96 ms |
19052 KB |
Output is correct |
91 |
Correct |
94 ms |
18764 KB |
Output is correct |
92 |
Correct |
96 ms |
18732 KB |
Output is correct |