#include "books.h"
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <limits.h>
#define LL long long
#define INF INT_MAX
using namespace std;
int cyc_n;
int cyc_idx[1111111];
int cyc_l[1111111],cyc_r[1111111];
int par[1111111];
int cyc_par[1111111];
int cyc_ldist[1111111],cyc_rdist[1111111];
int cyc_dist[1111111];
int cyc_pos[1111111];
int find_(int x) {
if(par[x]!=x) return par[x]=find_(par[x]);
return x;
}
long long minimum_walk(std::vector<int> P, int s) {
int n=P.size();
LL dist1=0;
int l=n,r=-1;
for(int i=0;i<n;i++){
dist1+=abs(P[i]-i);
if(P[i]!=i) l=min(l,i),r=max(r,i);
}
if(!dist1) return 0;
for(int i=0;i<n;i++){
if(cyc_idx[i]){
cyc_r[cyc_idx[i]]=i;
continue;
}
cyc_idx[i]=++cyc_n;
par[cyc_idx[i]]=cyc_idx[i];
cyc_dist[cyc_idx[i]]=INF;
cyc_l[cyc_idx[i]]=cyc_r[cyc_idx[i]]=i;
int j=P[i];
while(i!=j){
cyc_idx[j]=cyc_idx[i];
j=P[j];
}
}
vector<int> st;
for(int i=0;i<n;i++){
int rt=find_(cyc_idx[i]);
if(cyc_l[rt]==i) st.push_back(rt);
if(!st.empty() && st.back()!=rt){
par[st.back()]=rt;
cyc_l[rt]=min(cyc_l[rt],cyc_l[st.back()]);
cyc_r[rt]=max(cyc_r[rt],cyc_r[st.back()]);
st.pop_back();
}
if(cyc_r[rt]==i) st.pop_back();
}
for(int i=0;i<n;i++){
int rt=find_(cyc_idx[i]);
if(cyc_l[rt]==i){
if(!st.empty()) cyc_par[rt]=st.back();
st.push_back(rt);
}
if(cyc_r[rt]==i) st.pop_back();
}
for(int i=0;i<n;i++){
int rt=find_(cyc_idx[i]);
cyc_pos[rt]=i;
if(cyc_l[rt]==i) cyc_ldist[rt]=i-cyc_pos[cyc_par[rt]];
cyc_pos[cyc_par[rt]]=max(cyc_pos[cyc_par[rt]],i-cyc_ldist[rt]);
cyc_pos[rt]=i;
}
for(int i=n;i--;){
int rt=find_(cyc_idx[i]);
cyc_pos[rt]=i;
if(cyc_r[rt]==i) cyc_rdist[rt]=cyc_pos[cyc_par[rt]]-i;
cyc_pos[cyc_par[rt]]=min(cyc_pos[cyc_par[rt]],i+cyc_rdist[rt]);
cyc_pos[rt]=i;
}
int x=-1;
LL dist2=0;
for(int i=0;i<n;i++){
int rt=find_(cyc_idx[i]);
if(!cyc_par[rt] && cyc_l[rt]==i && cyc_l[rt]!=cyc_r[rt]){
cyc_dist[rt]=0;
if(x!=-1) dist2+=cyc_l[rt]-x;
x=cyc_r[rt];
}
else cyc_dist[rt]=min(cyc_ldist[rt],cyc_rdist[rt]);
}
LL dist3=0;
int rt=find_(cyc_idx[s]);
if(cyc_par[rt]){
while(cyc_par[rt]){
dist3+=cyc_dist[rt];
rt=cyc_par[rt];
}
}
else if(s<l) dist3=l-s;
else if(r<s) dist3=s-r;
return dist1+((dist2+dist3)<<1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
199 ms |
18936 KB |
Output is correct |
31 |
Correct |
202 ms |
18936 KB |
Output is correct |
32 |
Correct |
112 ms |
14840 KB |
Output is correct |
33 |
Correct |
185 ms |
40416 KB |
Output is correct |
34 |
Correct |
184 ms |
40564 KB |
Output is correct |
35 |
Correct |
184 ms |
36088 KB |
Output is correct |
36 |
Correct |
174 ms |
26872 KB |
Output is correct |
37 |
Correct |
157 ms |
19960 KB |
Output is correct |
38 |
Correct |
154 ms |
18988 KB |
Output is correct |
39 |
Correct |
157 ms |
19064 KB |
Output is correct |
40 |
Correct |
166 ms |
18852 KB |
Output is correct |
41 |
Correct |
191 ms |
18996 KB |
Output is correct |
42 |
Correct |
191 ms |
18808 KB |
Output is correct |
43 |
Correct |
173 ms |
42104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
476 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
21 |
Correct |
2 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
199 ms |
18936 KB |
Output is correct |
31 |
Correct |
202 ms |
18936 KB |
Output is correct |
32 |
Correct |
112 ms |
14840 KB |
Output is correct |
33 |
Correct |
185 ms |
40416 KB |
Output is correct |
34 |
Correct |
184 ms |
40564 KB |
Output is correct |
35 |
Correct |
184 ms |
36088 KB |
Output is correct |
36 |
Correct |
174 ms |
26872 KB |
Output is correct |
37 |
Correct |
157 ms |
19960 KB |
Output is correct |
38 |
Correct |
154 ms |
18988 KB |
Output is correct |
39 |
Correct |
157 ms |
19064 KB |
Output is correct |
40 |
Correct |
166 ms |
18852 KB |
Output is correct |
41 |
Correct |
191 ms |
18996 KB |
Output is correct |
42 |
Correct |
191 ms |
18808 KB |
Output is correct |
43 |
Correct |
173 ms |
42104 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
376 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
2 ms |
476 KB |
Output is correct |
50 |
Correct |
2 ms |
376 KB |
Output is correct |
51 |
Correct |
2 ms |
376 KB |
Output is correct |
52 |
Correct |
2 ms |
376 KB |
Output is correct |
53 |
Correct |
2 ms |
256 KB |
Output is correct |
54 |
Correct |
2 ms |
376 KB |
Output is correct |
55 |
Correct |
2 ms |
380 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
2 ms |
376 KB |
Output is correct |
59 |
Correct |
2 ms |
376 KB |
Output is correct |
60 |
Correct |
2 ms |
376 KB |
Output is correct |
61 |
Correct |
2 ms |
376 KB |
Output is correct |
62 |
Correct |
3 ms |
376 KB |
Output is correct |
63 |
Correct |
2 ms |
376 KB |
Output is correct |
64 |
Correct |
174 ms |
41764 KB |
Output is correct |
65 |
Correct |
162 ms |
44064 KB |
Output is correct |
66 |
Correct |
157 ms |
19872 KB |
Output is correct |
67 |
Correct |
153 ms |
19360 KB |
Output is correct |
68 |
Correct |
18 ms |
2936 KB |
Output is correct |
69 |
Correct |
17 ms |
2552 KB |
Output is correct |
70 |
Correct |
17 ms |
3320 KB |
Output is correct |
71 |
Correct |
19 ms |
4344 KB |
Output is correct |
72 |
Correct |
17 ms |
4856 KB |
Output is correct |
73 |
Correct |
17 ms |
2168 KB |
Output is correct |
74 |
Correct |
171 ms |
40484 KB |
Output is correct |
75 |
Correct |
175 ms |
40568 KB |
Output is correct |
76 |
Correct |
174 ms |
41592 KB |
Output is correct |
77 |
Correct |
176 ms |
41904 KB |
Output is correct |
78 |
Correct |
175 ms |
38620 KB |
Output is correct |
79 |
Correct |
178 ms |
38776 KB |
Output is correct |
80 |
Correct |
112 ms |
14840 KB |
Output is correct |
81 |
Correct |
175 ms |
35952 KB |
Output is correct |
82 |
Correct |
152 ms |
35944 KB |
Output is correct |
83 |
Correct |
177 ms |
31056 KB |
Output is correct |
84 |
Correct |
180 ms |
28484 KB |
Output is correct |
85 |
Correct |
162 ms |
23208 KB |
Output is correct |
86 |
Correct |
198 ms |
20728 KB |
Output is correct |
87 |
Correct |
201 ms |
46080 KB |
Output is correct |
88 |
Correct |
172 ms |
41456 KB |
Output is correct |
89 |
Correct |
174 ms |
33496 KB |
Output is correct |
90 |
Correct |
155 ms |
19488 KB |
Output is correct |
91 |
Correct |
158 ms |
19064 KB |
Output is correct |
92 |
Correct |
160 ms |
19000 KB |
Output is correct |