#include "books.h"
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
queue<int> q1,q2;
const int N=1000050;
int max(int a, int b){ return a>b?a:b;}
int min(int a, int b){ return a>b?b:a;}
int abs(int x){ return x>0?x:-x;}
ll minimum_walk(vector<int> p, int s)
{
ll sol=0;
int n=p.size(),i,j,ml=n,mr=-1;
for(i=0;i<n;i++)
{
sol+=abs(p[i]-i);
if(p[i]!=i) ml=min(ml,i),mr=max(mr,i);
}
q1.push(s);
int myl=s,myr=s;
while(1)
{
while(q1.size())
{
int x=q1.front();q1.pop();
while(myl>p[x]) q1.push(--myl);
while(myr<p[x]) q1.push(++myr);
}
int nl=myl,nr=myr,addl=0,addr=0;
bool lf=0,rf=0;
while(1)
{
if(nl<=ml) break;
nl--;addl+=2;q2.push(nl);
while(q2.size())
{
int x=q2.front();q2.pop();
if(p[x]>s) lf=1;
while(nl>p[x]) q2.push(--nl);
}
if(lf) break;
}
while(1)
{
if(nr>=mr) break;
nr++;addr+=2;q2.push(nr);
while(q2.size())
{
int x=q2.front();q2.pop();
if(p[x]<s) rf=1;
while(nr<p[x]) q2.push(++nr);
}
if(rf) break;
}
if(lf && rf) sol+=min(addl,addr);
else{ sol+=addl+addr;break;}
while(myl>nl) q1.push(--myl);
while(myr<nr) q1.push(++myr);
}
return sol;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:16:19: warning: unused variable 'j' [-Wunused-variable]
int n=p.size(),i,j,ml=n,mr=-1;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
2 ms |
516 KB |
Output is correct |
13 |
Correct |
2 ms |
520 KB |
Output is correct |
14 |
Correct |
2 ms |
648 KB |
Output is correct |
15 |
Correct |
3 ms |
648 KB |
Output is correct |
16 |
Correct |
2 ms |
648 KB |
Output is correct |
17 |
Correct |
2 ms |
648 KB |
Output is correct |
18 |
Correct |
2 ms |
648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
2 ms |
516 KB |
Output is correct |
13 |
Correct |
2 ms |
520 KB |
Output is correct |
14 |
Correct |
2 ms |
648 KB |
Output is correct |
15 |
Correct |
3 ms |
648 KB |
Output is correct |
16 |
Correct |
2 ms |
648 KB |
Output is correct |
17 |
Correct |
2 ms |
648 KB |
Output is correct |
18 |
Correct |
2 ms |
648 KB |
Output is correct |
19 |
Correct |
2 ms |
648 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
2 ms |
648 KB |
Output is correct |
22 |
Correct |
2 ms |
648 KB |
Output is correct |
23 |
Correct |
2 ms |
648 KB |
Output is correct |
24 |
Correct |
2 ms |
648 KB |
Output is correct |
25 |
Correct |
2 ms |
648 KB |
Output is correct |
26 |
Correct |
2 ms |
648 KB |
Output is correct |
27 |
Correct |
2 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
700 KB |
Output is correct |
29 |
Correct |
2 ms |
700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
2 ms |
516 KB |
Output is correct |
13 |
Correct |
2 ms |
520 KB |
Output is correct |
14 |
Correct |
2 ms |
648 KB |
Output is correct |
15 |
Correct |
3 ms |
648 KB |
Output is correct |
16 |
Correct |
2 ms |
648 KB |
Output is correct |
17 |
Correct |
2 ms |
648 KB |
Output is correct |
18 |
Correct |
2 ms |
648 KB |
Output is correct |
19 |
Correct |
2 ms |
648 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
2 ms |
648 KB |
Output is correct |
22 |
Correct |
2 ms |
648 KB |
Output is correct |
23 |
Correct |
2 ms |
648 KB |
Output is correct |
24 |
Correct |
2 ms |
648 KB |
Output is correct |
25 |
Correct |
2 ms |
648 KB |
Output is correct |
26 |
Correct |
2 ms |
648 KB |
Output is correct |
27 |
Correct |
2 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
700 KB |
Output is correct |
29 |
Correct |
2 ms |
700 KB |
Output is correct |
30 |
Correct |
198 ms |
19416 KB |
Output is correct |
31 |
Correct |
175 ms |
26032 KB |
Output is correct |
32 |
Correct |
142 ms |
28820 KB |
Output is correct |
33 |
Correct |
180 ms |
35616 KB |
Output is correct |
34 |
Correct |
162 ms |
42240 KB |
Output is correct |
35 |
Correct |
158 ms |
48916 KB |
Output is correct |
36 |
Correct |
160 ms |
55716 KB |
Output is correct |
37 |
Correct |
161 ms |
61244 KB |
Output is correct |
38 |
Correct |
156 ms |
61256 KB |
Output is correct |
39 |
Correct |
155 ms |
61256 KB |
Output is correct |
40 |
Correct |
163 ms |
64368 KB |
Output is correct |
41 |
Correct |
203 ms |
73024 KB |
Output is correct |
42 |
Correct |
170 ms |
79180 KB |
Output is correct |
43 |
Correct |
165 ms |
88164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
88164 KB |
Output is correct |
2 |
Correct |
2 ms |
88164 KB |
Output is correct |
3 |
Correct |
2 ms |
88164 KB |
Output is correct |
4 |
Correct |
2 ms |
88164 KB |
Output is correct |
5 |
Correct |
2 ms |
88164 KB |
Output is correct |
6 |
Correct |
2 ms |
88164 KB |
Output is correct |
7 |
Correct |
2 ms |
88164 KB |
Output is correct |
8 |
Correct |
2 ms |
88164 KB |
Output is correct |
9 |
Correct |
2 ms |
88164 KB |
Output is correct |
10 |
Correct |
2 ms |
88164 KB |
Output is correct |
11 |
Correct |
2 ms |
88164 KB |
Output is correct |
12 |
Correct |
2 ms |
88164 KB |
Output is correct |
13 |
Correct |
2 ms |
88164 KB |
Output is correct |
14 |
Correct |
2 ms |
88164 KB |
Output is correct |
15 |
Correct |
2 ms |
88164 KB |
Output is correct |
16 |
Correct |
2 ms |
88164 KB |
Output is correct |
17 |
Correct |
2 ms |
88164 KB |
Output is correct |
18 |
Correct |
3 ms |
88164 KB |
Output is correct |
19 |
Correct |
3 ms |
88164 KB |
Output is correct |
20 |
Correct |
3 ms |
88164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
488 KB |
Output is correct |
4 |
Correct |
2 ms |
488 KB |
Output is correct |
5 |
Correct |
2 ms |
488 KB |
Output is correct |
6 |
Correct |
2 ms |
516 KB |
Output is correct |
7 |
Correct |
2 ms |
516 KB |
Output is correct |
8 |
Correct |
2 ms |
516 KB |
Output is correct |
9 |
Correct |
2 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
516 KB |
Output is correct |
12 |
Correct |
2 ms |
516 KB |
Output is correct |
13 |
Correct |
2 ms |
520 KB |
Output is correct |
14 |
Correct |
2 ms |
648 KB |
Output is correct |
15 |
Correct |
3 ms |
648 KB |
Output is correct |
16 |
Correct |
2 ms |
648 KB |
Output is correct |
17 |
Correct |
2 ms |
648 KB |
Output is correct |
18 |
Correct |
2 ms |
648 KB |
Output is correct |
19 |
Correct |
2 ms |
648 KB |
Output is correct |
20 |
Correct |
2 ms |
648 KB |
Output is correct |
21 |
Correct |
2 ms |
648 KB |
Output is correct |
22 |
Correct |
2 ms |
648 KB |
Output is correct |
23 |
Correct |
2 ms |
648 KB |
Output is correct |
24 |
Correct |
2 ms |
648 KB |
Output is correct |
25 |
Correct |
2 ms |
648 KB |
Output is correct |
26 |
Correct |
2 ms |
648 KB |
Output is correct |
27 |
Correct |
2 ms |
700 KB |
Output is correct |
28 |
Correct |
2 ms |
700 KB |
Output is correct |
29 |
Correct |
2 ms |
700 KB |
Output is correct |
30 |
Correct |
198 ms |
19416 KB |
Output is correct |
31 |
Correct |
175 ms |
26032 KB |
Output is correct |
32 |
Correct |
142 ms |
28820 KB |
Output is correct |
33 |
Correct |
180 ms |
35616 KB |
Output is correct |
34 |
Correct |
162 ms |
42240 KB |
Output is correct |
35 |
Correct |
158 ms |
48916 KB |
Output is correct |
36 |
Correct |
160 ms |
55716 KB |
Output is correct |
37 |
Correct |
161 ms |
61244 KB |
Output is correct |
38 |
Correct |
156 ms |
61256 KB |
Output is correct |
39 |
Correct |
155 ms |
61256 KB |
Output is correct |
40 |
Correct |
163 ms |
64368 KB |
Output is correct |
41 |
Correct |
203 ms |
73024 KB |
Output is correct |
42 |
Correct |
170 ms |
79180 KB |
Output is correct |
43 |
Correct |
165 ms |
88164 KB |
Output is correct |
44 |
Correct |
2 ms |
88164 KB |
Output is correct |
45 |
Correct |
2 ms |
88164 KB |
Output is correct |
46 |
Correct |
2 ms |
88164 KB |
Output is correct |
47 |
Correct |
2 ms |
88164 KB |
Output is correct |
48 |
Correct |
2 ms |
88164 KB |
Output is correct |
49 |
Correct |
2 ms |
88164 KB |
Output is correct |
50 |
Correct |
2 ms |
88164 KB |
Output is correct |
51 |
Correct |
2 ms |
88164 KB |
Output is correct |
52 |
Correct |
2 ms |
88164 KB |
Output is correct |
53 |
Correct |
2 ms |
88164 KB |
Output is correct |
54 |
Correct |
2 ms |
88164 KB |
Output is correct |
55 |
Correct |
2 ms |
88164 KB |
Output is correct |
56 |
Correct |
2 ms |
88164 KB |
Output is correct |
57 |
Correct |
2 ms |
88164 KB |
Output is correct |
58 |
Correct |
2 ms |
88164 KB |
Output is correct |
59 |
Correct |
2 ms |
88164 KB |
Output is correct |
60 |
Correct |
2 ms |
88164 KB |
Output is correct |
61 |
Correct |
3 ms |
88164 KB |
Output is correct |
62 |
Correct |
3 ms |
88164 KB |
Output is correct |
63 |
Correct |
3 ms |
88164 KB |
Output is correct |
64 |
Correct |
321 ms |
95268 KB |
Output is correct |
65 |
Correct |
290 ms |
101820 KB |
Output is correct |
66 |
Correct |
295 ms |
108512 KB |
Output is correct |
67 |
Correct |
330 ms |
114560 KB |
Output is correct |
68 |
Correct |
28 ms |
114560 KB |
Output is correct |
69 |
Correct |
28 ms |
114560 KB |
Output is correct |
70 |
Correct |
28 ms |
114560 KB |
Output is correct |
71 |
Correct |
27 ms |
114560 KB |
Output is correct |
72 |
Correct |
26 ms |
114560 KB |
Output is correct |
73 |
Correct |
30 ms |
114560 KB |
Output is correct |
74 |
Correct |
288 ms |
121620 KB |
Output is correct |
75 |
Correct |
274 ms |
128368 KB |
Output is correct |
76 |
Correct |
262 ms |
139068 KB |
Output is correct |
77 |
Correct |
282 ms |
145772 KB |
Output is correct |
78 |
Correct |
245 ms |
149928 KB |
Output is correct |
79 |
Correct |
303 ms |
156708 KB |
Output is correct |
80 |
Correct |
202 ms |
162084 KB |
Output is correct |
81 |
Correct |
274 ms |
170220 KB |
Output is correct |
82 |
Correct |
244 ms |
176952 KB |
Output is correct |
83 |
Correct |
263 ms |
182312 KB |
Output is correct |
84 |
Correct |
302 ms |
189056 KB |
Output is correct |
85 |
Correct |
270 ms |
195804 KB |
Output is correct |
86 |
Correct |
254 ms |
202544 KB |
Output is correct |
87 |
Correct |
279 ms |
209288 KB |
Output is correct |
88 |
Correct |
289 ms |
216040 KB |
Output is correct |
89 |
Correct |
296 ms |
222924 KB |
Output is correct |
90 |
Correct |
304 ms |
229524 KB |
Output is correct |
91 |
Correct |
301 ms |
236284 KB |
Output is correct |
92 |
Correct |
307 ms |
243252 KB |
Output is correct |