#include "books.h"
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
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;
if(p[cur]!=cur){
en=max(en,cur);
bg=min(bg,cur);
}
if(vis[cur]) continue;
cycle.clear();
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(curl>ml){
curl--;
mr=max(mr,r[curl]);
ml=min(ml,l[curl]);
}else if(curr<mr){
curr++;
mr=max(mr,r[curr]);
ml=min(ml,l[curr]);
}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 |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
3 ms |
8180 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8280 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
2 ms |
8304 KB |
Output is correct |
14 |
Correct |
2 ms |
8280 KB |
Output is correct |
15 |
Correct |
2 ms |
8280 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
3 ms |
8180 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8280 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
2 ms |
8304 KB |
Output is correct |
14 |
Correct |
2 ms |
8280 KB |
Output is correct |
15 |
Correct |
2 ms |
8280 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8536 KB |
Output is correct |
20 |
Correct |
3 ms |
8280 KB |
Output is correct |
21 |
Correct |
3 ms |
8288 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8280 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
2 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8092 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
2 ms |
8284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
3 ms |
8180 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8280 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
2 ms |
8304 KB |
Output is correct |
14 |
Correct |
2 ms |
8280 KB |
Output is correct |
15 |
Correct |
2 ms |
8280 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8536 KB |
Output is correct |
20 |
Correct |
3 ms |
8280 KB |
Output is correct |
21 |
Correct |
3 ms |
8288 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8280 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
2 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8092 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
2 ms |
8284 KB |
Output is correct |
30 |
Correct |
110 ms |
21252 KB |
Output is correct |
31 |
Correct |
114 ms |
19204 KB |
Output is correct |
32 |
Correct |
83 ms |
17036 KB |
Output is correct |
33 |
Correct |
92 ms |
17052 KB |
Output is correct |
34 |
Correct |
84 ms |
17056 KB |
Output is correct |
35 |
Correct |
104 ms |
16980 KB |
Output is correct |
36 |
Correct |
84 ms |
17044 KB |
Output is correct |
37 |
Correct |
99 ms |
17040 KB |
Output is correct |
38 |
Correct |
89 ms |
16976 KB |
Output is correct |
39 |
Correct |
81 ms |
16976 KB |
Output is correct |
40 |
Correct |
85 ms |
17164 KB |
Output is correct |
41 |
Correct |
94 ms |
18948 KB |
Output is correct |
42 |
Correct |
102 ms |
17856 KB |
Output is correct |
43 |
Correct |
83 ms |
17040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8284 KB |
Output is correct |
2 |
Correct |
2 ms |
8148 KB |
Output is correct |
3 |
Correct |
2 ms |
8284 KB |
Output is correct |
4 |
Correct |
2 ms |
8284 KB |
Output is correct |
5 |
Correct |
2 ms |
8280 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8284 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
3 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8204 KB |
Output is correct |
13 |
Correct |
2 ms |
8280 KB |
Output is correct |
14 |
Correct |
2 ms |
8284 KB |
Output is correct |
15 |
Correct |
2 ms |
8284 KB |
Output is correct |
16 |
Correct |
3 ms |
8284 KB |
Output is correct |
17 |
Correct |
3 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8280 KB |
Output is correct |
20 |
Correct |
3 ms |
8280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8280 KB |
Output is correct |
2 |
Correct |
2 ms |
8028 KB |
Output is correct |
3 |
Correct |
3 ms |
8180 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8284 KB |
Output is correct |
6 |
Correct |
3 ms |
8284 KB |
Output is correct |
7 |
Correct |
2 ms |
8284 KB |
Output is correct |
8 |
Correct |
2 ms |
8284 KB |
Output is correct |
9 |
Correct |
2 ms |
8280 KB |
Output is correct |
10 |
Correct |
2 ms |
8284 KB |
Output is correct |
11 |
Correct |
2 ms |
8284 KB |
Output is correct |
12 |
Correct |
3 ms |
8280 KB |
Output is correct |
13 |
Correct |
2 ms |
8304 KB |
Output is correct |
14 |
Correct |
2 ms |
8280 KB |
Output is correct |
15 |
Correct |
2 ms |
8280 KB |
Output is correct |
16 |
Correct |
2 ms |
8284 KB |
Output is correct |
17 |
Correct |
2 ms |
8284 KB |
Output is correct |
18 |
Correct |
3 ms |
8284 KB |
Output is correct |
19 |
Correct |
2 ms |
8536 KB |
Output is correct |
20 |
Correct |
3 ms |
8280 KB |
Output is correct |
21 |
Correct |
3 ms |
8288 KB |
Output is correct |
22 |
Correct |
2 ms |
8284 KB |
Output is correct |
23 |
Correct |
3 ms |
8284 KB |
Output is correct |
24 |
Correct |
3 ms |
8280 KB |
Output is correct |
25 |
Correct |
3 ms |
8284 KB |
Output is correct |
26 |
Correct |
2 ms |
8284 KB |
Output is correct |
27 |
Correct |
2 ms |
8092 KB |
Output is correct |
28 |
Correct |
3 ms |
8284 KB |
Output is correct |
29 |
Correct |
2 ms |
8284 KB |
Output is correct |
30 |
Correct |
110 ms |
21252 KB |
Output is correct |
31 |
Correct |
114 ms |
19204 KB |
Output is correct |
32 |
Correct |
83 ms |
17036 KB |
Output is correct |
33 |
Correct |
92 ms |
17052 KB |
Output is correct |
34 |
Correct |
84 ms |
17056 KB |
Output is correct |
35 |
Correct |
104 ms |
16980 KB |
Output is correct |
36 |
Correct |
84 ms |
17044 KB |
Output is correct |
37 |
Correct |
99 ms |
17040 KB |
Output is correct |
38 |
Correct |
89 ms |
16976 KB |
Output is correct |
39 |
Correct |
81 ms |
16976 KB |
Output is correct |
40 |
Correct |
85 ms |
17164 KB |
Output is correct |
41 |
Correct |
94 ms |
18948 KB |
Output is correct |
42 |
Correct |
102 ms |
17856 KB |
Output is correct |
43 |
Correct |
83 ms |
17040 KB |
Output is correct |
44 |
Correct |
2 ms |
8284 KB |
Output is correct |
45 |
Correct |
2 ms |
8148 KB |
Output is correct |
46 |
Correct |
2 ms |
8284 KB |
Output is correct |
47 |
Correct |
2 ms |
8284 KB |
Output is correct |
48 |
Correct |
2 ms |
8280 KB |
Output is correct |
49 |
Correct |
3 ms |
8284 KB |
Output is correct |
50 |
Correct |
2 ms |
8284 KB |
Output is correct |
51 |
Correct |
2 ms |
8284 KB |
Output is correct |
52 |
Correct |
2 ms |
8284 KB |
Output is correct |
53 |
Correct |
2 ms |
8284 KB |
Output is correct |
54 |
Correct |
3 ms |
8284 KB |
Output is correct |
55 |
Correct |
3 ms |
8204 KB |
Output is correct |
56 |
Correct |
2 ms |
8280 KB |
Output is correct |
57 |
Correct |
2 ms |
8284 KB |
Output is correct |
58 |
Correct |
2 ms |
8284 KB |
Output is correct |
59 |
Correct |
3 ms |
8284 KB |
Output is correct |
60 |
Correct |
3 ms |
8284 KB |
Output is correct |
61 |
Correct |
3 ms |
8284 KB |
Output is correct |
62 |
Correct |
2 ms |
8280 KB |
Output is correct |
63 |
Correct |
3 ms |
8280 KB |
Output is correct |
64 |
Correct |
81 ms |
17036 KB |
Output is correct |
65 |
Correct |
96 ms |
16976 KB |
Output is correct |
66 |
Correct |
81 ms |
16944 KB |
Output is correct |
67 |
Correct |
92 ms |
17048 KB |
Output is correct |
68 |
Correct |
11 ms |
9052 KB |
Output is correct |
69 |
Correct |
10 ms |
9148 KB |
Output is correct |
70 |
Correct |
10 ms |
9052 KB |
Output is correct |
71 |
Correct |
10 ms |
9164 KB |
Output is correct |
72 |
Correct |
11 ms |
8928 KB |
Output is correct |
73 |
Correct |
15 ms |
9188 KB |
Output is correct |
74 |
Correct |
86 ms |
17060 KB |
Output is correct |
75 |
Correct |
82 ms |
16968 KB |
Output is correct |
76 |
Correct |
93 ms |
16976 KB |
Output is correct |
77 |
Correct |
81 ms |
17032 KB |
Output is correct |
78 |
Correct |
87 ms |
16980 KB |
Output is correct |
79 |
Correct |
91 ms |
17040 KB |
Output is correct |
80 |
Correct |
84 ms |
17040 KB |
Output is correct |
81 |
Execution timed out |
2025 ms |
17036 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |