#include "bits/stdc++.h"
using namespace std;
vector<int> P;
int vis[1000001];
int vis2[1000001];
int L[1000001];
int R[1000001];
int mn = 1e9 ,mx = -1e9;
void dfs(int i){
vis[i] = 1;
mn = min(mn,i);
mx = max(mx,i);
if(!vis[P[i]])dfs(P[i]);
}
void DFS(int i){
L[i] = mn;
R[i] = mx;
vis2[i] = 1;
if(!vis2[P[i]])DFS(P[i]);
}
void extend(int &l,int &r){
int mnl = l;
int mnr = r;
mnl = min({mnl,L[l],L[r]});
mnr = max({mnr,R[r],R[l]});
while(l>mnl||r<mnr){
if(l>mnl){
l--;
mnl = min(mnl,L[l]);
mnr = max(mnr,R[l]);
}else{
r++;
mnl = min(mnl,L[r]);
mnr = max(mnr,R[r]);
}
}
}
int get(int l,int r,int targl,int targr){
extend(l,r);
int all = 0;
while(l>targl||r<targr){
extend(l,r);
int lL = l;
int rL = r;
int cost1 = 0 , cost2 = 0;
bool found1 = 0 , found2 = 0;
while(lL>=targl){
extend(lL,rL);
if(rL>r){found1 = 1;break;}
if(lL==targl){
break;
}
cost1+=2;
lL--;
}
int lR = l;
int rR = r;
while(rR<=targr){
extend(lR,rR);
if(lR<l){found2 = 1;break;}
if(rR==targr){
break;
}
cost2+=2;
rR++;
}
if(found1&&found2){
all+=min(cost1,cost2);
}else{
all+=cost1+cost2;
}
l = min({l,lL,lR});
r = max({r,rL,rR});
}
return all;
}
long long minimum_walk(vector<int> p, int s){
long long ans = 0;
int n = p.size();
P.clear();
for(int i = 0;i<n;i++){
vis[i] = 0;vis2[i] = 0;
P.push_back(p[i]);
ans+=abs(i-p[i]);
}
vector<pair<int,int>> lol;
int rngl = s;
int rngr = s;
for(int i = 0;i<n;i++){
if(!vis[i]){
mn = 1e9 , mx = -1e9;
dfs(i);
DFS(i);
if(i!=P[i]){
rngl = min(rngl,mn);
rngr = max(rngr,mx);
}
}
}
ans+=get(s,s,rngl,rngr);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
6492 KB |
Output is correct |
3 |
Correct |
0 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6592 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6596 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
0 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
6492 KB |
Output is correct |
3 |
Correct |
0 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6592 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6596 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
0 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
6492 KB |
Output is correct |
3 |
Correct |
0 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6592 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6596 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
0 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
90 ms |
34644 KB |
Output is correct |
31 |
Correct |
92 ms |
34492 KB |
Output is correct |
32 |
Correct |
71 ms |
34492 KB |
Output is correct |
33 |
Correct |
73 ms |
34536 KB |
Output is correct |
34 |
Correct |
76 ms |
34496 KB |
Output is correct |
35 |
Correct |
71 ms |
34492 KB |
Output is correct |
36 |
Correct |
69 ms |
34540 KB |
Output is correct |
37 |
Correct |
64 ms |
34492 KB |
Output is correct |
38 |
Correct |
68 ms |
34532 KB |
Output is correct |
39 |
Correct |
65 ms |
34496 KB |
Output is correct |
40 |
Correct |
74 ms |
34520 KB |
Output is correct |
41 |
Correct |
86 ms |
34496 KB |
Output is correct |
42 |
Correct |
92 ms |
34660 KB |
Output is correct |
43 |
Correct |
72 ms |
34492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6488 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6592 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
0 ms |
6492 KB |
Output is correct |
3 |
Correct |
0 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6592 KB |
Output is correct |
6 |
Correct |
1 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
0 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6596 KB |
Output is correct |
11 |
Correct |
0 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
0 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
90 ms |
34644 KB |
Output is correct |
31 |
Correct |
92 ms |
34492 KB |
Output is correct |
32 |
Correct |
71 ms |
34492 KB |
Output is correct |
33 |
Correct |
73 ms |
34536 KB |
Output is correct |
34 |
Correct |
76 ms |
34496 KB |
Output is correct |
35 |
Correct |
71 ms |
34492 KB |
Output is correct |
36 |
Correct |
69 ms |
34540 KB |
Output is correct |
37 |
Correct |
64 ms |
34492 KB |
Output is correct |
38 |
Correct |
68 ms |
34532 KB |
Output is correct |
39 |
Correct |
65 ms |
34496 KB |
Output is correct |
40 |
Correct |
74 ms |
34520 KB |
Output is correct |
41 |
Correct |
86 ms |
34496 KB |
Output is correct |
42 |
Correct |
92 ms |
34660 KB |
Output is correct |
43 |
Correct |
72 ms |
34492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
1 ms |
6488 KB |
Output is correct |
48 |
Correct |
1 ms |
6492 KB |
Output is correct |
49 |
Correct |
1 ms |
6492 KB |
Output is correct |
50 |
Correct |
1 ms |
6492 KB |
Output is correct |
51 |
Correct |
1 ms |
6492 KB |
Output is correct |
52 |
Correct |
1 ms |
6492 KB |
Output is correct |
53 |
Correct |
1 ms |
6492 KB |
Output is correct |
54 |
Correct |
1 ms |
6492 KB |
Output is correct |
55 |
Correct |
1 ms |
6492 KB |
Output is correct |
56 |
Correct |
1 ms |
6492 KB |
Output is correct |
57 |
Correct |
1 ms |
6492 KB |
Output is correct |
58 |
Correct |
1 ms |
6492 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6488 KB |
Output is correct |
61 |
Correct |
1 ms |
6492 KB |
Output is correct |
62 |
Correct |
1 ms |
6592 KB |
Output is correct |
63 |
Correct |
1 ms |
6492 KB |
Output is correct |
64 |
Correct |
64 ms |
34660 KB |
Output is correct |
65 |
Correct |
80 ms |
34712 KB |
Output is correct |
66 |
Correct |
65 ms |
34664 KB |
Output is correct |
67 |
Correct |
65 ms |
34544 KB |
Output is correct |
68 |
Correct |
8 ms |
12756 KB |
Output is correct |
69 |
Correct |
7 ms |
12756 KB |
Output is correct |
70 |
Correct |
7 ms |
12548 KB |
Output is correct |
71 |
Correct |
7 ms |
12604 KB |
Output is correct |
72 |
Correct |
7 ms |
12724 KB |
Output is correct |
73 |
Correct |
8 ms |
12756 KB |
Output is correct |
74 |
Correct |
78 ms |
34536 KB |
Output is correct |
75 |
Correct |
70 ms |
34544 KB |
Output is correct |
76 |
Correct |
67 ms |
34492 KB |
Output is correct |
77 |
Correct |
76 ms |
34496 KB |
Output is correct |
78 |
Correct |
71 ms |
34528 KB |
Output is correct |
79 |
Correct |
71 ms |
34536 KB |
Output is correct |
80 |
Correct |
61 ms |
34660 KB |
Output is correct |
81 |
Correct |
75 ms |
34492 KB |
Output is correct |
82 |
Correct |
67 ms |
34492 KB |
Output is correct |
83 |
Correct |
68 ms |
34660 KB |
Output is correct |
84 |
Correct |
67 ms |
34548 KB |
Output is correct |
85 |
Correct |
68 ms |
34752 KB |
Output is correct |
86 |
Correct |
66 ms |
34672 KB |
Output is correct |
87 |
Correct |
75 ms |
34540 KB |
Output is correct |
88 |
Correct |
71 ms |
34492 KB |
Output is correct |
89 |
Correct |
73 ms |
34704 KB |
Output is correct |
90 |
Correct |
65 ms |
34492 KB |
Output is correct |
91 |
Correct |
67 ms |
34496 KB |
Output is correct |
92 |
Correct |
69 ms |
34540 KB |
Output is correct |