#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e6;
const ll inf = 2e9;
bool vis[N];
vector <int> points;
int l[N],r[N];
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
ll ans = 0;
ll mn2 = s;
ll mx2 = s;
for(ll i = 0;i < n;i++){
ans+=abs(i - p[i]);
if(i != p[i]){
mx2 = max(mx2,i);
mn2 = min(mn2,i);
}
if(!vis[i]){
points.clear();
int nr = i;
int mx = -inf;
int mn = inf;
while(!vis[nr]){
vis[nr] = 1;
mx = max(mx,nr);
mn = min(mn,nr);
points.push_back(nr);
nr = p[nr];
}
for(auto j:points){
l[j] = mn;
r[j] = mx;
}
}
}
int curl,curr,exl,exr;
curl = s;exl = l[s];
curr = s;exr = r[s];
//cout<<mn2<<' '<<mx2<<' '<<exl<<' '<<exr<<' '<<curl<<' '<<curr<<'\n';
while(exl > mn2 || exr < mx2){
if(curl > exl){
curl--;
exl = min(exl,l[curl]);
exr = max(exr,r[curl]);
}else if(curr < exr){
curr++;
exl = min(exl,l[curr]);
exr = max(exr,r[curr]);
}else{
//cout<<curl<<' '<<curr<<'\n';
int costl = 0;
bool ok = 0;
int x = exl;
int exl2 = exl;
while(x > mn2){
exl2 = min(exl2,l[x]);
if(x == exl2){
costl+=2;
exl2--;
}
x--;
if(r[x] > exr){
ok = 1;
break;
}
}
int costr = 0;
int exr2 = exr;
x = exr;
while(x < mx2){
exr2 = max(exr2,r[x]);
if(x == exr2){
costr+=2;
exr2++;
}
x++;
if(l[x] < exl){
ok = 1;
break;
}
}
if(ok){
if(costl > costr){
exr = exr2;
ans+=costr;
}else{
exl = exl2;
ans+=costl;
}
}else{
ans+=costl;
ans+=costr;
break;
}
}
}
return ans;
}
/**
3 1
2 1 0
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4484 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4484 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4696 KB |
Output is correct |
30 |
Correct |
107 ms |
20664 KB |
Output is correct |
31 |
Correct |
100 ms |
18544 KB |
Output is correct |
32 |
Correct |
70 ms |
16896 KB |
Output is correct |
33 |
Correct |
109 ms |
17080 KB |
Output is correct |
34 |
Correct |
81 ms |
16996 KB |
Output is correct |
35 |
Correct |
81 ms |
17060 KB |
Output is correct |
36 |
Correct |
112 ms |
16976 KB |
Output is correct |
37 |
Correct |
76 ms |
16980 KB |
Output is correct |
38 |
Correct |
76 ms |
16980 KB |
Output is correct |
39 |
Correct |
75 ms |
16912 KB |
Output is correct |
40 |
Correct |
83 ms |
17348 KB |
Output is correct |
41 |
Correct |
93 ms |
18804 KB |
Output is correct |
42 |
Correct |
94 ms |
17596 KB |
Output is correct |
43 |
Correct |
82 ms |
16980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4532 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4696 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4544 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4536 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4484 KB |
Output is correct |
28 |
Correct |
2 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4696 KB |
Output is correct |
30 |
Correct |
107 ms |
20664 KB |
Output is correct |
31 |
Correct |
100 ms |
18544 KB |
Output is correct |
32 |
Correct |
70 ms |
16896 KB |
Output is correct |
33 |
Correct |
109 ms |
17080 KB |
Output is correct |
34 |
Correct |
81 ms |
16996 KB |
Output is correct |
35 |
Correct |
81 ms |
17060 KB |
Output is correct |
36 |
Correct |
112 ms |
16976 KB |
Output is correct |
37 |
Correct |
76 ms |
16980 KB |
Output is correct |
38 |
Correct |
76 ms |
16980 KB |
Output is correct |
39 |
Correct |
75 ms |
16912 KB |
Output is correct |
40 |
Correct |
83 ms |
17348 KB |
Output is correct |
41 |
Correct |
93 ms |
18804 KB |
Output is correct |
42 |
Correct |
94 ms |
17596 KB |
Output is correct |
43 |
Correct |
82 ms |
16980 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
52 |
Correct |
1 ms |
4444 KB |
Output is correct |
53 |
Correct |
1 ms |
4444 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4532 KB |
Output is correct |
56 |
Correct |
1 ms |
4444 KB |
Output is correct |
57 |
Correct |
1 ms |
4444 KB |
Output is correct |
58 |
Correct |
1 ms |
4696 KB |
Output is correct |
59 |
Correct |
1 ms |
4444 KB |
Output is correct |
60 |
Correct |
1 ms |
4444 KB |
Output is correct |
61 |
Correct |
1 ms |
4544 KB |
Output is correct |
62 |
Correct |
1 ms |
4440 KB |
Output is correct |
63 |
Correct |
1 ms |
4444 KB |
Output is correct |
64 |
Correct |
83 ms |
23680 KB |
Output is correct |
65 |
Correct |
104 ms |
23680 KB |
Output is correct |
66 |
Correct |
106 ms |
23796 KB |
Output is correct |
67 |
Correct |
89 ms |
23676 KB |
Output is correct |
68 |
Correct |
9 ms |
10180 KB |
Output is correct |
69 |
Correct |
9 ms |
10128 KB |
Output is correct |
70 |
Correct |
9 ms |
10076 KB |
Output is correct |
71 |
Correct |
9 ms |
10076 KB |
Output is correct |
72 |
Correct |
10 ms |
10132 KB |
Output is correct |
73 |
Correct |
9 ms |
10076 KB |
Output is correct |
74 |
Correct |
84 ms |
23640 KB |
Output is correct |
75 |
Correct |
120 ms |
23692 KB |
Output is correct |
76 |
Correct |
84 ms |
23672 KB |
Output is correct |
77 |
Correct |
79 ms |
23796 KB |
Output is correct |
78 |
Correct |
84 ms |
23636 KB |
Output is correct |
79 |
Correct |
95 ms |
23772 KB |
Output is correct |
80 |
Correct |
72 ms |
23632 KB |
Output is correct |
81 |
Correct |
75 ms |
23636 KB |
Output is correct |
82 |
Correct |
76 ms |
23636 KB |
Output is correct |
83 |
Correct |
85 ms |
23636 KB |
Output is correct |
84 |
Correct |
89 ms |
23768 KB |
Output is correct |
85 |
Correct |
82 ms |
23680 KB |
Output is correct |
86 |
Correct |
80 ms |
23684 KB |
Output is correct |
87 |
Correct |
82 ms |
23804 KB |
Output is correct |
88 |
Correct |
96 ms |
23920 KB |
Output is correct |
89 |
Correct |
82 ms |
23636 KB |
Output is correct |
90 |
Correct |
81 ms |
23640 KB |
Output is correct |
91 |
Correct |
79 ms |
23632 KB |
Output is correct |
92 |
Correct |
81 ms |
23892 KB |
Output is correct |