#include <bits/stdc++.h>
#include "books.h"
using namespace std;
bool vis[1000005];
vector<int> l(1000000),r(1000000);
long long minimum_walk(std::vector<int> p, int s){
int n=p.size();
long long ans=0;
int en=s;
int bg=s;
//cout << ans<<endl;
vector<int> cycle;
for (int i = 0; i < n; ++i)
{
ans+=abs(p[i]-i);
int cur=i;
cycle.clear();
if(p[cur]!=cur){
en=max(en,cur);
bg=min(bg,cur);
}
if(vis[cur]) continue;
int mn=1e6+5;
int mx=-1;
while(vis[cur]==0){
cycle.push_back(cur);
vis[cur]=1;
mx=max(mx,cur);
mn=min(mn,cur);
cur=p[cur];
}
for(int u:cycle){
//cout <<u<<" ";
l[u]=mn;
r[u]=mx;
}
}
//cout <<"nabba"<<endl;
int curl=s;int ml=l[s];
int curr=s;int mr=r[s];
while(curr<en||curl>bg){
//cout <<curl<<" "<<curr<<endl;
if(curr<mr){
curr++;
mr=max(mr,r[curr]);
ml=min(ml,l[curr]);
}else if(curl>ml){
curl--;
mr=max(mr,r[curl]);
ml=min(ml,l[curl]);
}else{
//cout <<"hey"<<endl;
bool ok=0;
int costl=0;
int x=mr;
int cl=mr;
while(x>bg){
cl=min(cl,l[x]);
if(x==cl){
costl+=2;
cl--;
}
x--;
if(r[x]>mr){
ok=true;
break;
}
}
x=ml;
int cr=ml;
int costr=0;
while(x<en){
cr=max(cr,r[x]);
if(x==cr){
cr++;
costr+=2;
}
x++;
if(l[x]<ml){
ok=true;
break;
}
}
if(ok){
if(costl>costr){
mr=cr;
ans+=costr;
}else{
ml=cl;
ans+=costl;
}
}else{
ans+=costl+costr;
break;
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
3 ms |
8028 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
3 ms |
8028 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Correct |
2 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
2 ms |
8280 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
3 ms |
8028 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Correct |
2 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
2 ms |
8280 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
108 ms |
21520 KB |
Output is correct |
31 |
Correct |
103 ms |
19204 KB |
Output is correct |
32 |
Correct |
76 ms |
17040 KB |
Output is correct |
33 |
Correct |
95 ms |
17040 KB |
Output is correct |
34 |
Correct |
97 ms |
16980 KB |
Output is correct |
35 |
Correct |
105 ms |
16976 KB |
Output is correct |
36 |
Correct |
94 ms |
16980 KB |
Output is correct |
37 |
Correct |
88 ms |
17044 KB |
Output is correct |
38 |
Correct |
95 ms |
16980 KB |
Output is correct |
39 |
Correct |
81 ms |
17044 KB |
Output is correct |
40 |
Correct |
85 ms |
17216 KB |
Output is correct |
41 |
Correct |
100 ms |
18956 KB |
Output is correct |
42 |
Correct |
97 ms |
17740 KB |
Output is correct |
43 |
Correct |
98 ms |
16980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
3 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
3 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8284 KB |
Output is correct |
10 |
Correct |
4 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8284 KB |
Output is correct |
14 |
Correct |
2 ms |
8280 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8280 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8280 KB |
Output is correct |
19 |
Correct |
2 ms |
8284 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8028 KB |
Output is correct |
2 |
Correct |
3 ms |
8284 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8284 KB |
Output is correct |
7 |
Correct |
3 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
3 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8284 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
2 ms |
8024 KB |
Output is correct |
17 |
Correct |
3 ms |
8028 KB |
Output is correct |
18 |
Correct |
2 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
2 ms |
8284 KB |
Output is correct |
21 |
Correct |
2 ms |
8284 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
2 ms |
8280 KB |
Output is correct |
24 |
Correct |
2 ms |
8284 KB |
Output is correct |
25 |
Correct |
2 ms |
8284 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8280 KB |
Output is correct |
28 |
Correct |
2 ms |
8284 KB |
Output is correct |
29 |
Correct |
3 ms |
8284 KB |
Output is correct |
30 |
Correct |
108 ms |
21520 KB |
Output is correct |
31 |
Correct |
103 ms |
19204 KB |
Output is correct |
32 |
Correct |
76 ms |
17040 KB |
Output is correct |
33 |
Correct |
95 ms |
17040 KB |
Output is correct |
34 |
Correct |
97 ms |
16980 KB |
Output is correct |
35 |
Correct |
105 ms |
16976 KB |
Output is correct |
36 |
Correct |
94 ms |
16980 KB |
Output is correct |
37 |
Correct |
88 ms |
17044 KB |
Output is correct |
38 |
Correct |
95 ms |
16980 KB |
Output is correct |
39 |
Correct |
81 ms |
17044 KB |
Output is correct |
40 |
Correct |
85 ms |
17216 KB |
Output is correct |
41 |
Correct |
100 ms |
18956 KB |
Output is correct |
42 |
Correct |
97 ms |
17740 KB |
Output is correct |
43 |
Correct |
98 ms |
16980 KB |
Output is correct |
44 |
Correct |
2 ms |
8284 KB |
Output is correct |
45 |
Correct |
3 ms |
8284 KB |
Output is correct |
46 |
Correct |
2 ms |
8284 KB |
Output is correct |
47 |
Correct |
3 ms |
8284 KB |
Output is correct |
48 |
Correct |
2 ms |
8284 KB |
Output is correct |
49 |
Correct |
2 ms |
8284 KB |
Output is correct |
50 |
Correct |
3 ms |
8284 KB |
Output is correct |
51 |
Correct |
3 ms |
8284 KB |
Output is correct |
52 |
Correct |
2 ms |
8284 KB |
Output is correct |
53 |
Correct |
4 ms |
8284 KB |
Output is correct |
54 |
Correct |
3 ms |
8284 KB |
Output is correct |
55 |
Correct |
3 ms |
8284 KB |
Output is correct |
56 |
Correct |
2 ms |
8284 KB |
Output is correct |
57 |
Correct |
2 ms |
8280 KB |
Output is correct |
58 |
Correct |
2 ms |
8284 KB |
Output is correct |
59 |
Correct |
2 ms |
8280 KB |
Output is correct |
60 |
Correct |
2 ms |
8284 KB |
Output is correct |
61 |
Correct |
3 ms |
8280 KB |
Output is correct |
62 |
Correct |
2 ms |
8284 KB |
Output is correct |
63 |
Correct |
2 ms |
8284 KB |
Output is correct |
64 |
Correct |
83 ms |
17052 KB |
Output is correct |
65 |
Correct |
82 ms |
17056 KB |
Output is correct |
66 |
Correct |
82 ms |
17036 KB |
Output is correct |
67 |
Correct |
82 ms |
16980 KB |
Output is correct |
68 |
Correct |
11 ms |
9052 KB |
Output is correct |
69 |
Correct |
10 ms |
8932 KB |
Output is correct |
70 |
Correct |
11 ms |
8932 KB |
Output is correct |
71 |
Correct |
10 ms |
9052 KB |
Output is correct |
72 |
Correct |
12 ms |
9052 KB |
Output is correct |
73 |
Correct |
10 ms |
9184 KB |
Output is correct |
74 |
Correct |
82 ms |
17044 KB |
Output is correct |
75 |
Correct |
90 ms |
16976 KB |
Output is correct |
76 |
Correct |
83 ms |
17096 KB |
Output is correct |
77 |
Correct |
82 ms |
17040 KB |
Output is correct |
78 |
Correct |
85 ms |
17044 KB |
Output is correct |
79 |
Correct |
87 ms |
17040 KB |
Output is correct |
80 |
Correct |
75 ms |
17040 KB |
Output is correct |
81 |
Execution timed out |
2069 ms |
17032 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |