#include "books.h"
#include <bits/stdc++.h>
#define ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
const ll inf=1e9;
long long minimum_walk(vector<int> p, int s) {
ll i;
ll n=p.sz;
vll l(n,-1);
vll r(n,-1);
ll mn=inf,mx=0;
vll cur;
long long ans=0;
function<void(ll)> get_cycle = [&](ll v){
if(cur.sz && v==cur[0])return;
cur.pb(v);
get_cycle(p[v]);
};
for(i=0;i<n;i++){
ans+=abs(i-p[i]);
if(l[i]!=-1)continue;
get_cycle(i);
sort(all(cur));
for(auto j : cur){
l[j]=cur[0];
r[j]=cur.bc;
}
if(cur.sz>1){
mn=min(mn,cur[0]);
mx=max(mx,cur.bc);
}
cur.clear();
}
ll L=s,R=s;
vll v;
vector<bool> vis(n,0);
while(L>mn || R<mx){
while(v.size()){
ll x=v.bc;
v.pob;
while(L>l[x]){
L--;
if(!vis[l[L]]){
vis[l[L]]=1;
v.pb(L);
}
}
while(R<r[x]){
R++;
if(!vis[l[R]]){
vis[l[R]]=1;
v.pb(R);
}
}
}
ll nl=L,nr=R;
ll free=L;
ll c=0,c2=0;
while(nl>mn){
if(r[nl]>R) break;
free=min(free,l[nl]);
if(free==nl) c+=2;
nl--;
}
free=R;
while(nr<mx){
if(l[nr]<L)break;
free=max(free,r[nr]);
if(free==nr) c2+=2;
nr++;
}
if(l[nr]<L && r[nl]>R){
if(c<c2){
ans+=c;
L=nl;
v.pb(nl);
}else{
ans+=c2;
R=nr;
v.pb(nr);
}
}else if(r[nl]>R){
R=nr;
ans+=c2;
ans+=c;
L=nl;
v.pb(nl);
}else{
L=nl;
ans+=c;
ans+=c2;
R=nr;
v.pb(nr);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
159 ms |
43628 KB |
Output is correct |
31 |
Correct |
157 ms |
36444 KB |
Output is correct |
32 |
Correct |
71 ms |
22704 KB |
Output is correct |
33 |
Correct |
84 ms |
22864 KB |
Output is correct |
34 |
Correct |
77 ms |
22772 KB |
Output is correct |
35 |
Correct |
72 ms |
22868 KB |
Output is correct |
36 |
Correct |
75 ms |
22868 KB |
Output is correct |
37 |
Correct |
82 ms |
22864 KB |
Output is correct |
38 |
Correct |
90 ms |
23120 KB |
Output is correct |
39 |
Correct |
93 ms |
23124 KB |
Output is correct |
40 |
Correct |
109 ms |
24172 KB |
Output is correct |
41 |
Correct |
137 ms |
34736 KB |
Output is correct |
42 |
Correct |
122 ms |
28360 KB |
Output is correct |
43 |
Correct |
70 ms |
22868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
600 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
159 ms |
43628 KB |
Output is correct |
31 |
Correct |
157 ms |
36444 KB |
Output is correct |
32 |
Correct |
71 ms |
22704 KB |
Output is correct |
33 |
Correct |
84 ms |
22864 KB |
Output is correct |
34 |
Correct |
77 ms |
22772 KB |
Output is correct |
35 |
Correct |
72 ms |
22868 KB |
Output is correct |
36 |
Correct |
75 ms |
22868 KB |
Output is correct |
37 |
Correct |
82 ms |
22864 KB |
Output is correct |
38 |
Correct |
90 ms |
23120 KB |
Output is correct |
39 |
Correct |
93 ms |
23124 KB |
Output is correct |
40 |
Correct |
109 ms |
24172 KB |
Output is correct |
41 |
Correct |
137 ms |
34736 KB |
Output is correct |
42 |
Correct |
122 ms |
28360 KB |
Output is correct |
43 |
Correct |
70 ms |
22868 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
344 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
344 KB |
Output is correct |
64 |
Correct |
75 ms |
27588 KB |
Output is correct |
65 |
Correct |
84 ms |
28648 KB |
Output is correct |
66 |
Correct |
98 ms |
23120 KB |
Output is correct |
67 |
Correct |
87 ms |
23020 KB |
Output is correct |
68 |
Correct |
8 ms |
2648 KB |
Output is correct |
69 |
Correct |
8 ms |
2484 KB |
Output is correct |
70 |
Correct |
8 ms |
2908 KB |
Output is correct |
71 |
Correct |
8 ms |
2908 KB |
Output is correct |
72 |
Correct |
7 ms |
2652 KB |
Output is correct |
73 |
Correct |
15 ms |
2972 KB |
Output is correct |
74 |
Correct |
70 ms |
22872 KB |
Output is correct |
75 |
Correct |
77 ms |
22896 KB |
Output is correct |
76 |
Correct |
95 ms |
27484 KB |
Output is correct |
77 |
Correct |
76 ms |
27240 KB |
Output is correct |
78 |
Correct |
81 ms |
24036 KB |
Output is correct |
79 |
Correct |
79 ms |
23500 KB |
Output is correct |
80 |
Correct |
74 ms |
22764 KB |
Output is correct |
81 |
Correct |
73 ms |
23500 KB |
Output is correct |
82 |
Correct |
71 ms |
23400 KB |
Output is correct |
83 |
Correct |
81 ms |
22864 KB |
Output is correct |
84 |
Correct |
76 ms |
22868 KB |
Output is correct |
85 |
Correct |
81 ms |
22868 KB |
Output is correct |
86 |
Correct |
83 ms |
22864 KB |
Output is correct |
87 |
Correct |
88 ms |
22884 KB |
Output is correct |
88 |
Correct |
83 ms |
22900 KB |
Output is correct |
89 |
Correct |
76 ms |
22864 KB |
Output is correct |
90 |
Correct |
107 ms |
22868 KB |
Output is correct |
91 |
Correct |
93 ms |
22868 KB |
Output is correct |
92 |
Correct |
109 ms |
23276 KB |
Output is correct |