#include <bits/stdc++.h>
//#include "books.h"
using namespace std;
long long minimum_walk(std::vector<int> p, int s){
int n=p.size();
long long ans=0;
bool vis[n];
memset(vis,0,sizeof vis);
vector<int> l(n),r(n);
for (int i = 0; i < n; ++i)
{
ans+=abs(p[i]-i);
}
int en=s;
int bg=s;
//cout << ans<<endl;
for (int i = 0; i < n; ++i)
{
int cur=i;
if(p[cur]!=cur){
en=max(en,cur);
bg=min(bg,cur);
}
if(vis[cur]) continue;
int mn=1e6+5;
int mx=-1;
vector<int> cycle;
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;
}
# |
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 |
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 |
1 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 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 |
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 |
1 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
500 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 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 |
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 |
1 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
500 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
103 ms |
27876 KB |
Output is correct |
31 |
Correct |
118 ms |
25836 KB |
Output is correct |
32 |
Correct |
96 ms |
23776 KB |
Output is correct |
33 |
Correct |
102 ms |
23636 KB |
Output is correct |
34 |
Correct |
111 ms |
23780 KB |
Output is correct |
35 |
Correct |
125 ms |
23644 KB |
Output is correct |
36 |
Correct |
97 ms |
23660 KB |
Output is correct |
37 |
Correct |
83 ms |
23780 KB |
Output is correct |
38 |
Correct |
82 ms |
23796 KB |
Output is correct |
39 |
Correct |
98 ms |
23640 KB |
Output is correct |
40 |
Correct |
86 ms |
23996 KB |
Output is correct |
41 |
Correct |
106 ms |
26684 KB |
Output is correct |
42 |
Correct |
95 ms |
25200 KB |
Output is correct |
43 |
Correct |
98 ms |
23776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 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 |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
432 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 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 |
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 |
1 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 |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
500 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
103 ms |
27876 KB |
Output is correct |
31 |
Correct |
118 ms |
25836 KB |
Output is correct |
32 |
Correct |
96 ms |
23776 KB |
Output is correct |
33 |
Correct |
102 ms |
23636 KB |
Output is correct |
34 |
Correct |
111 ms |
23780 KB |
Output is correct |
35 |
Correct |
125 ms |
23644 KB |
Output is correct |
36 |
Correct |
97 ms |
23660 KB |
Output is correct |
37 |
Correct |
83 ms |
23780 KB |
Output is correct |
38 |
Correct |
82 ms |
23796 KB |
Output is correct |
39 |
Correct |
98 ms |
23640 KB |
Output is correct |
40 |
Correct |
86 ms |
23996 KB |
Output is correct |
41 |
Correct |
106 ms |
26684 KB |
Output is correct |
42 |
Correct |
95 ms |
25200 KB |
Output is correct |
43 |
Correct |
98 ms |
23776 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 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 |
436 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
344 KB |
Output is correct |
54 |
Correct |
1 ms |
432 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
97 ms |
23800 KB |
Output is correct |
65 |
Correct |
107 ms |
23560 KB |
Output is correct |
66 |
Correct |
84 ms |
23616 KB |
Output is correct |
67 |
Correct |
94 ms |
23788 KB |
Output is correct |
68 |
Correct |
9 ms |
2652 KB |
Output is correct |
69 |
Correct |
9 ms |
2688 KB |
Output is correct |
70 |
Correct |
9 ms |
2652 KB |
Output is correct |
71 |
Correct |
10 ms |
2940 KB |
Output is correct |
72 |
Correct |
10 ms |
2652 KB |
Output is correct |
73 |
Correct |
9 ms |
2652 KB |
Output is correct |
74 |
Correct |
104 ms |
23792 KB |
Output is correct |
75 |
Correct |
99 ms |
23788 KB |
Output is correct |
76 |
Correct |
102 ms |
23784 KB |
Output is correct |
77 |
Correct |
117 ms |
23784 KB |
Output is correct |
78 |
Correct |
104 ms |
23796 KB |
Output is correct |
79 |
Correct |
101 ms |
23620 KB |
Output is correct |
80 |
Correct |
90 ms |
23636 KB |
Output is correct |
81 |
Execution timed out |
2040 ms |
23792 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |