#include "books.h"
#include<bits/stdc++.h>
#define ll int
#define pb push_back
#define mp make_pair
#define ld long double
#define F first
#define S second
#define pii pair<ll,ll>
using namespace :: std;
const ll maxn=1e6+500;
const ll inf=1e9+900;
const ll logg=20;
vector<ll> p;
bool vis[maxn];
ll d[maxn];
ll last=-1,fir,N;
ll cnt=0;
ll mxx[maxn];
ll mnn[maxn];
ll l[maxn];
ll r[maxn];
map<pii,ll> ml;
ll ras[maxn];
vector<pii> ger[maxn];
ll fas[maxn];
void addYal(ll a,ll b,ll w){
ger[a].pb(mp(b,w));
}
void bfs(ll a){
fill(fas,fas+maxn,inf);
deque<ll> qu;
qu.pb(a);
fas[a]=0;
while(qu.size()){
ll v=qu.back();
qu.pop_back();
for(auto e:ger[v]){
if(fas[e.F]==inf){
fas[e.F]=fas[v]+e.S;
if(e.S==0){
qu.pb(e.F);
}else{
qu.push_front(e.F);
}
}
}
}
}
void dfs(ll a){
if(vis[a])return;
vis[a]=2;
d[a]=cnt;
dfs(p[a]);
}
long long minimum_walk(vector<int> P, int s){
p=P;
ll n=p.size();
N=n;
long long ans=0;
for(ll i=0;i<n;i++){
if(!vis[i]){
dfs(i);
cnt++;
}
if(p[i]!=i)last=i;
ans+=abs(i-p[i]);
}
for(ll i=n-1;i>=0;i--){
if(p[i]!=i){
fir=i;
}
}
if(last==-1)return 0;
fill(mnn,mnn+maxn,inf);
fill(mxx,mxx+maxn,0);
for(ll i=0;i<n;i++){
mnn[d[i]]=min(mnn[d[i]],i);
mxx[d[i]]=max(mxx[d[i]],i);
}
if(s<=fir){
ll t=s;
for(ll i=s;i<=last;i++){
if(t<i){
ans+=2;
t=max(t,i);
}
t=max(t,mxx[d[i]]);
}
return ans;
}
if(s>=last){
ll t=s;
for(ll i=s;i>=fir;i--){
if(i<t){
ans+=2;
t=min(t,i);
}
t=min(t,mnn[d[i]]);
}
return ans;
}
ll minn,maxx;
minn=fir;
maxx=fir;
for(ll i=fir;i<s;i++){
minn=min(minn,mnn[d[i]]);
maxx=max(maxx,mxx[d[i]]);
if(maxx==i){
ans+=2;
fir=i+1;
}
}
minn=last;
maxx=last;
for(ll i=last;i>s;i--){
minn=min(minn,mnn[d[i]]);
maxx=max(maxx,mxx[d[i]]);
if(minn==i){
ans+=2;
last=i-1;
}
}
minn=s;
maxx=s;
for(ll i=s;i<n;i++){
minn=min(minn,mnn[d[i]]);
maxx=max(maxx,mxx[d[i]]);
l[i]=minn;
r[i]=maxx;
}
minn=s;
maxx=s;
for(ll i=s;i>=0;i--){
minn=min(minn,mnn[d[i]]);
maxx=max(maxx,mxx[d[i]]);
l[i]=minn;
r[i]=maxx;
}
for(ll i=0;i<n;i++){
l[i]=max(l[i],fir);
r[i]=min(r[i],last);
}
cnt=0;
for(ll i=fir;i<=last;i++){
if(ml.count(mp(l[i],r[i]))==0){
ml[mp(l[i],r[i])]=cnt++;
}
ras[i]=ml[mp(l[i],r[i])];
}
ll start=ras[s];
for(ll i=fir;i<=last;i++){
ll v=ml[mp(l[i],r[i])];
addYal(v,ras[l[i]],0);
addYal(v,ras[r[i]],0);
if(i!=fir){
addYal(v,ras[i-1],1);
}
if(i!=last){
addYal(v,ras[i+1],1);
}
}
bfs(start);
ll res=min(fas[ras[fir]],fas[ras[last]]);
return ans+res*2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31736 KB |
Output is correct |
3 |
Correct |
30 ms |
31736 KB |
Output is correct |
4 |
Correct |
30 ms |
31728 KB |
Output is correct |
5 |
Correct |
31 ms |
31608 KB |
Output is correct |
6 |
Correct |
30 ms |
31736 KB |
Output is correct |
7 |
Correct |
30 ms |
31608 KB |
Output is correct |
8 |
Correct |
30 ms |
31608 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
30 ms |
31608 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
30 ms |
31652 KB |
Output is correct |
13 |
Correct |
30 ms |
31608 KB |
Output is correct |
14 |
Correct |
22 ms |
23800 KB |
Output is correct |
15 |
Correct |
30 ms |
31736 KB |
Output is correct |
16 |
Correct |
30 ms |
31736 KB |
Output is correct |
17 |
Correct |
31 ms |
31708 KB |
Output is correct |
18 |
Correct |
30 ms |
31608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31736 KB |
Output is correct |
3 |
Correct |
30 ms |
31736 KB |
Output is correct |
4 |
Correct |
30 ms |
31728 KB |
Output is correct |
5 |
Correct |
31 ms |
31608 KB |
Output is correct |
6 |
Correct |
30 ms |
31736 KB |
Output is correct |
7 |
Correct |
30 ms |
31608 KB |
Output is correct |
8 |
Correct |
30 ms |
31608 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
30 ms |
31608 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
30 ms |
31652 KB |
Output is correct |
13 |
Correct |
30 ms |
31608 KB |
Output is correct |
14 |
Correct |
22 ms |
23800 KB |
Output is correct |
15 |
Correct |
30 ms |
31736 KB |
Output is correct |
16 |
Correct |
30 ms |
31736 KB |
Output is correct |
17 |
Correct |
31 ms |
31708 KB |
Output is correct |
18 |
Correct |
30 ms |
31608 KB |
Output is correct |
19 |
Correct |
30 ms |
31736 KB |
Output is correct |
20 |
Correct |
34 ms |
31752 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
30 ms |
31704 KB |
Output is correct |
23 |
Correct |
30 ms |
31696 KB |
Output is correct |
24 |
Correct |
31 ms |
31736 KB |
Output is correct |
25 |
Correct |
31 ms |
31744 KB |
Output is correct |
26 |
Correct |
30 ms |
31736 KB |
Output is correct |
27 |
Correct |
30 ms |
31736 KB |
Output is correct |
28 |
Correct |
37 ms |
31736 KB |
Output is correct |
29 |
Correct |
30 ms |
31736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31736 KB |
Output is correct |
3 |
Correct |
30 ms |
31736 KB |
Output is correct |
4 |
Correct |
30 ms |
31728 KB |
Output is correct |
5 |
Correct |
31 ms |
31608 KB |
Output is correct |
6 |
Correct |
30 ms |
31736 KB |
Output is correct |
7 |
Correct |
30 ms |
31608 KB |
Output is correct |
8 |
Correct |
30 ms |
31608 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
30 ms |
31608 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
30 ms |
31652 KB |
Output is correct |
13 |
Correct |
30 ms |
31608 KB |
Output is correct |
14 |
Correct |
22 ms |
23800 KB |
Output is correct |
15 |
Correct |
30 ms |
31736 KB |
Output is correct |
16 |
Correct |
30 ms |
31736 KB |
Output is correct |
17 |
Correct |
31 ms |
31708 KB |
Output is correct |
18 |
Correct |
30 ms |
31608 KB |
Output is correct |
19 |
Correct |
30 ms |
31736 KB |
Output is correct |
20 |
Correct |
34 ms |
31752 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
30 ms |
31704 KB |
Output is correct |
23 |
Correct |
30 ms |
31696 KB |
Output is correct |
24 |
Correct |
31 ms |
31736 KB |
Output is correct |
25 |
Correct |
31 ms |
31744 KB |
Output is correct |
26 |
Correct |
30 ms |
31736 KB |
Output is correct |
27 |
Correct |
30 ms |
31736 KB |
Output is correct |
28 |
Correct |
37 ms |
31736 KB |
Output is correct |
29 |
Correct |
30 ms |
31736 KB |
Output is correct |
30 |
Correct |
243 ms |
48300 KB |
Output is correct |
31 |
Correct |
262 ms |
48344 KB |
Output is correct |
32 |
Correct |
162 ms |
40532 KB |
Output is correct |
33 |
Correct |
189 ms |
48248 KB |
Output is correct |
34 |
Correct |
189 ms |
48412 KB |
Output is correct |
35 |
Correct |
192 ms |
48368 KB |
Output is correct |
36 |
Correct |
186 ms |
48248 KB |
Output is correct |
37 |
Correct |
184 ms |
48280 KB |
Output is correct |
38 |
Correct |
188 ms |
48376 KB |
Output is correct |
39 |
Correct |
188 ms |
48288 KB |
Output is correct |
40 |
Correct |
199 ms |
48248 KB |
Output is correct |
41 |
Correct |
229 ms |
48412 KB |
Output is correct |
42 |
Correct |
233 ms |
48248 KB |
Output is correct |
43 |
Correct |
189 ms |
48248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
35732 KB |
Output is correct |
2 |
Correct |
34 ms |
35704 KB |
Output is correct |
3 |
Correct |
41 ms |
35704 KB |
Output is correct |
4 |
Correct |
34 ms |
35708 KB |
Output is correct |
5 |
Correct |
34 ms |
35564 KB |
Output is correct |
6 |
Correct |
34 ms |
35676 KB |
Output is correct |
7 |
Correct |
34 ms |
35684 KB |
Output is correct |
8 |
Correct |
34 ms |
35704 KB |
Output is correct |
9 |
Correct |
35 ms |
35704 KB |
Output is correct |
10 |
Correct |
24 ms |
23928 KB |
Output is correct |
11 |
Correct |
35 ms |
35708 KB |
Output is correct |
12 |
Correct |
35 ms |
35704 KB |
Output is correct |
13 |
Correct |
35 ms |
35832 KB |
Output is correct |
14 |
Correct |
35 ms |
35704 KB |
Output is correct |
15 |
Correct |
34 ms |
35704 KB |
Output is correct |
16 |
Correct |
34 ms |
35676 KB |
Output is correct |
17 |
Correct |
35 ms |
35704 KB |
Output is correct |
18 |
Correct |
35 ms |
35704 KB |
Output is correct |
19 |
Correct |
34 ms |
35704 KB |
Output is correct |
20 |
Correct |
34 ms |
35752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
31608 KB |
Output is correct |
2 |
Correct |
30 ms |
31736 KB |
Output is correct |
3 |
Correct |
30 ms |
31736 KB |
Output is correct |
4 |
Correct |
30 ms |
31728 KB |
Output is correct |
5 |
Correct |
31 ms |
31608 KB |
Output is correct |
6 |
Correct |
30 ms |
31736 KB |
Output is correct |
7 |
Correct |
30 ms |
31608 KB |
Output is correct |
8 |
Correct |
30 ms |
31608 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
30 ms |
31608 KB |
Output is correct |
11 |
Correct |
23 ms |
23800 KB |
Output is correct |
12 |
Correct |
30 ms |
31652 KB |
Output is correct |
13 |
Correct |
30 ms |
31608 KB |
Output is correct |
14 |
Correct |
22 ms |
23800 KB |
Output is correct |
15 |
Correct |
30 ms |
31736 KB |
Output is correct |
16 |
Correct |
30 ms |
31736 KB |
Output is correct |
17 |
Correct |
31 ms |
31708 KB |
Output is correct |
18 |
Correct |
30 ms |
31608 KB |
Output is correct |
19 |
Correct |
30 ms |
31736 KB |
Output is correct |
20 |
Correct |
34 ms |
31752 KB |
Output is correct |
21 |
Correct |
23 ms |
23800 KB |
Output is correct |
22 |
Correct |
30 ms |
31704 KB |
Output is correct |
23 |
Correct |
30 ms |
31696 KB |
Output is correct |
24 |
Correct |
31 ms |
31736 KB |
Output is correct |
25 |
Correct |
31 ms |
31744 KB |
Output is correct |
26 |
Correct |
30 ms |
31736 KB |
Output is correct |
27 |
Correct |
30 ms |
31736 KB |
Output is correct |
28 |
Correct |
37 ms |
31736 KB |
Output is correct |
29 |
Correct |
30 ms |
31736 KB |
Output is correct |
30 |
Correct |
243 ms |
48300 KB |
Output is correct |
31 |
Correct |
262 ms |
48344 KB |
Output is correct |
32 |
Correct |
162 ms |
40532 KB |
Output is correct |
33 |
Correct |
189 ms |
48248 KB |
Output is correct |
34 |
Correct |
189 ms |
48412 KB |
Output is correct |
35 |
Correct |
192 ms |
48368 KB |
Output is correct |
36 |
Correct |
186 ms |
48248 KB |
Output is correct |
37 |
Correct |
184 ms |
48280 KB |
Output is correct |
38 |
Correct |
188 ms |
48376 KB |
Output is correct |
39 |
Correct |
188 ms |
48288 KB |
Output is correct |
40 |
Correct |
199 ms |
48248 KB |
Output is correct |
41 |
Correct |
229 ms |
48412 KB |
Output is correct |
42 |
Correct |
233 ms |
48248 KB |
Output is correct |
43 |
Correct |
189 ms |
48248 KB |
Output is correct |
44 |
Correct |
34 ms |
35732 KB |
Output is correct |
45 |
Correct |
34 ms |
35704 KB |
Output is correct |
46 |
Correct |
41 ms |
35704 KB |
Output is correct |
47 |
Correct |
34 ms |
35708 KB |
Output is correct |
48 |
Correct |
34 ms |
35564 KB |
Output is correct |
49 |
Correct |
34 ms |
35676 KB |
Output is correct |
50 |
Correct |
34 ms |
35684 KB |
Output is correct |
51 |
Correct |
34 ms |
35704 KB |
Output is correct |
52 |
Correct |
35 ms |
35704 KB |
Output is correct |
53 |
Correct |
24 ms |
23928 KB |
Output is correct |
54 |
Correct |
35 ms |
35708 KB |
Output is correct |
55 |
Correct |
35 ms |
35704 KB |
Output is correct |
56 |
Correct |
35 ms |
35832 KB |
Output is correct |
57 |
Correct |
35 ms |
35704 KB |
Output is correct |
58 |
Correct |
34 ms |
35704 KB |
Output is correct |
59 |
Correct |
34 ms |
35676 KB |
Output is correct |
60 |
Correct |
35 ms |
35704 KB |
Output is correct |
61 |
Correct |
35 ms |
35704 KB |
Output is correct |
62 |
Correct |
34 ms |
35704 KB |
Output is correct |
63 |
Correct |
34 ms |
35752 KB |
Output is correct |
64 |
Correct |
445 ms |
102304 KB |
Output is correct |
65 |
Correct |
491 ms |
108732 KB |
Output is correct |
66 |
Correct |
314 ms |
107076 KB |
Output is correct |
67 |
Correct |
320 ms |
116148 KB |
Output is correct |
68 |
Correct |
63 ms |
42592 KB |
Output is correct |
69 |
Correct |
61 ms |
42512 KB |
Output is correct |
70 |
Correct |
59 ms |
42528 KB |
Output is correct |
71 |
Correct |
61 ms |
42484 KB |
Output is correct |
72 |
Correct |
64 ms |
43256 KB |
Output is correct |
73 |
Correct |
60 ms |
42456 KB |
Output is correct |
74 |
Correct |
202 ms |
66832 KB |
Output is correct |
75 |
Correct |
201 ms |
66840 KB |
Output is correct |
76 |
Correct |
307 ms |
102104 KB |
Output is correct |
77 |
Correct |
307 ms |
102064 KB |
Output is correct |
78 |
Correct |
289 ms |
104896 KB |
Output is correct |
79 |
Correct |
298 ms |
105092 KB |
Output is correct |
80 |
Correct |
165 ms |
47256 KB |
Output is correct |
81 |
Correct |
1189 ms |
141232 KB |
Output is correct |
82 |
Correct |
1201 ms |
141204 KB |
Output is correct |
83 |
Correct |
956 ms |
129980 KB |
Output is correct |
84 |
Correct |
875 ms |
131580 KB |
Output is correct |
85 |
Correct |
635 ms |
111108 KB |
Output is correct |
86 |
Correct |
438 ms |
113216 KB |
Output is correct |
87 |
Correct |
1380 ms |
168292 KB |
Output is correct |
88 |
Correct |
1295 ms |
157220 KB |
Output is correct |
89 |
Correct |
1074 ms |
140036 KB |
Output is correct |
90 |
Correct |
353 ms |
114396 KB |
Output is correct |
91 |
Correct |
330 ms |
103128 KB |
Output is correct |
92 |
Correct |
362 ms |
102668 KB |
Output is correct |