#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4544 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 |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4548 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 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 |
4700 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 |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4544 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 |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4548 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 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 |
4700 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 |
4532 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 |
4548 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4544 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 |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4548 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 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 |
4700 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 |
4532 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 |
4548 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
127 ms |
26852 KB |
Output is correct |
31 |
Correct |
97 ms |
25280 KB |
Output is correct |
32 |
Correct |
74 ms |
23648 KB |
Output is correct |
33 |
Correct |
91 ms |
23676 KB |
Output is correct |
34 |
Correct |
91 ms |
23672 KB |
Output is correct |
35 |
Correct |
84 ms |
23808 KB |
Output is correct |
36 |
Correct |
81 ms |
23680 KB |
Output is correct |
37 |
Correct |
81 ms |
23632 KB |
Output is correct |
38 |
Correct |
86 ms |
23672 KB |
Output is correct |
39 |
Correct |
78 ms |
23632 KB |
Output is correct |
40 |
Correct |
82 ms |
24016 KB |
Output is correct |
41 |
Correct |
94 ms |
25580 KB |
Output is correct |
42 |
Correct |
88 ms |
24520 KB |
Output is correct |
43 |
Correct |
77 ms |
23636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 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 |
4532 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 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 |
4532 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4544 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 |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4548 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 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 |
4700 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 |
4532 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 |
4548 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
127 ms |
26852 KB |
Output is correct |
31 |
Correct |
97 ms |
25280 KB |
Output is correct |
32 |
Correct |
74 ms |
23648 KB |
Output is correct |
33 |
Correct |
91 ms |
23676 KB |
Output is correct |
34 |
Correct |
91 ms |
23672 KB |
Output is correct |
35 |
Correct |
84 ms |
23808 KB |
Output is correct |
36 |
Correct |
81 ms |
23680 KB |
Output is correct |
37 |
Correct |
81 ms |
23632 KB |
Output is correct |
38 |
Correct |
86 ms |
23672 KB |
Output is correct |
39 |
Correct |
78 ms |
23632 KB |
Output is correct |
40 |
Correct |
82 ms |
24016 KB |
Output is correct |
41 |
Correct |
94 ms |
25580 KB |
Output is correct |
42 |
Correct |
88 ms |
24520 KB |
Output is correct |
43 |
Correct |
77 ms |
23636 KB |
Output is correct |
44 |
Correct |
1 ms |
4440 KB |
Output is correct |
45 |
Correct |
1 ms |
4440 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 |
4532 KB |
Output is correct |
52 |
Correct |
1 ms |
4444 KB |
Output is correct |
53 |
Correct |
1 ms |
4700 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4696 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 |
4532 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 |
4444 KB |
Output is correct |
62 |
Correct |
1 ms |
4444 KB |
Output is correct |
63 |
Correct |
1 ms |
4444 KB |
Output is correct |
64 |
Correct |
107 ms |
23888 KB |
Output is correct |
65 |
Correct |
82 ms |
23808 KB |
Output is correct |
66 |
Correct |
79 ms |
23636 KB |
Output is correct |
67 |
Correct |
80 ms |
23636 KB |
Output is correct |
68 |
Correct |
9 ms |
10076 KB |
Output is correct |
69 |
Correct |
9 ms |
10388 KB |
Output is correct |
70 |
Correct |
11 ms |
10076 KB |
Output is correct |
71 |
Correct |
9 ms |
10076 KB |
Output is correct |
72 |
Correct |
9 ms |
10076 KB |
Output is correct |
73 |
Correct |
9 ms |
10076 KB |
Output is correct |
74 |
Correct |
81 ms |
23676 KB |
Output is correct |
75 |
Correct |
84 ms |
23560 KB |
Output is correct |
76 |
Correct |
83 ms |
23840 KB |
Output is correct |
77 |
Correct |
78 ms |
23808 KB |
Output is correct |
78 |
Correct |
79 ms |
23672 KB |
Output is correct |
79 |
Correct |
81 ms |
23556 KB |
Output is correct |
80 |
Correct |
72 ms |
23636 KB |
Output is correct |
81 |
Correct |
80 ms |
23632 KB |
Output is correct |
82 |
Correct |
76 ms |
23636 KB |
Output is correct |
83 |
Correct |
83 ms |
23552 KB |
Output is correct |
84 |
Correct |
107 ms |
23664 KB |
Output is correct |
85 |
Correct |
94 ms |
24180 KB |
Output is correct |
86 |
Correct |
81 ms |
23636 KB |
Output is correct |
87 |
Correct |
83 ms |
23556 KB |
Output is correct |
88 |
Correct |
84 ms |
23636 KB |
Output is correct |
89 |
Correct |
87 ms |
24056 KB |
Output is correct |
90 |
Correct |
78 ms |
23632 KB |
Output is correct |
91 |
Correct |
79 ms |
23636 KB |
Output is correct |
92 |
Correct |
83 ms |
23688 KB |
Output is correct |