#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define vi vector<int>
#define ii pair<int, int>
ii explore(int f1, int f2, int t2, int t1, vi &mi, vi &ma, vi &c) {
int f3 = f1;
int t3 = t1;
for(int i = f1; i <= t1; i++) {
if(i == f2) {
i = t2+1;
if(i > t1) continue;
}
f3 = min(f3, mi[c[i]]);
t3 = max(t3, ma[c[i]]);
}
if(f3 == f1 && t3 == t1) return {f1, t1};
return explore(f3, f1, t1, t3, mi, ma, c);
}
ll minimum_walk(vi p, int s) {
int n = p.size();
ll res = 0;
vi c(n,-1), mi(n, n), ma(n, 0);
for(int i = 0; i < n; i++) {
if(c[i] == -1) {
int k = p[i];
c[i] = i;
mi[i] = i;
ma[i] = i;
res += p[i] - i;
while(k != i) {
c[k] = i;
res += abs(p[k]-k);
mi[i] = min(mi[i], k);
ma[i] = max(ma[i], k);
k = p[k];
}
}
}
ii lr = explore(s,-1,-1,s,mi,ma,c);
int l = lr.F; int r = lr.S;
ll lc = 0; ll rc = 0;
while(l || r < n-1) {
bool b = 1;
if(l) {
lc += 2;
lr = explore(l-1,l,r,r,mi,ma,c);
l = lr.F;
if(lr.S > r) {
r = lr.S;
b = 0;
res += lc;
rc = 0;
lc = 0;
}
}
if(r < n-1 && b) {
rc += 2;
lr = explore(l,l,r,r+1,mi,ma,c);
r = lr.S;
if(lr.F < l) {
l = lr.F;
res += rc;
rc = 0;
lc = 0;
}
}
}
for(int i = 0; i < s; i++) {
if(p[i] == i) res -= 2;
else break;
}
for(int i = n-1; i > s; i--) {
if(p[i] == i) res -= 2;
else break;
}
return res + lc + rc;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
145 ms |
26588 KB |
Output is correct |
31 |
Correct |
210 ms |
26596 KB |
Output is correct |
32 |
Correct |
102 ms |
26572 KB |
Output is correct |
33 |
Correct |
106 ms |
26584 KB |
Output is correct |
34 |
Correct |
103 ms |
26592 KB |
Output is correct |
35 |
Correct |
113 ms |
26580 KB |
Output is correct |
36 |
Correct |
115 ms |
26464 KB |
Output is correct |
37 |
Correct |
100 ms |
26592 KB |
Output is correct |
38 |
Correct |
103 ms |
26572 KB |
Output is correct |
39 |
Correct |
113 ms |
26588 KB |
Output is correct |
40 |
Correct |
102 ms |
26572 KB |
Output is correct |
41 |
Correct |
114 ms |
26468 KB |
Output is correct |
42 |
Correct |
118 ms |
26584 KB |
Output is correct |
43 |
Correct |
107 ms |
26700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
296 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
296 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
145 ms |
26588 KB |
Output is correct |
31 |
Correct |
210 ms |
26596 KB |
Output is correct |
32 |
Correct |
102 ms |
26572 KB |
Output is correct |
33 |
Correct |
106 ms |
26584 KB |
Output is correct |
34 |
Correct |
103 ms |
26592 KB |
Output is correct |
35 |
Correct |
113 ms |
26580 KB |
Output is correct |
36 |
Correct |
115 ms |
26464 KB |
Output is correct |
37 |
Correct |
100 ms |
26592 KB |
Output is correct |
38 |
Correct |
103 ms |
26572 KB |
Output is correct |
39 |
Correct |
113 ms |
26588 KB |
Output is correct |
40 |
Correct |
102 ms |
26572 KB |
Output is correct |
41 |
Correct |
114 ms |
26468 KB |
Output is correct |
42 |
Correct |
118 ms |
26584 KB |
Output is correct |
43 |
Correct |
107 ms |
26700 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
2 ms |
296 KB |
Output is correct |
50 |
Correct |
1 ms |
300 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
212 KB |
Output is correct |
53 |
Correct |
1 ms |
300 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
300 KB |
Output is correct |
56 |
Correct |
1 ms |
300 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
1 ms |
212 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
300 KB |
Output is correct |
64 |
Correct |
98 ms |
26588 KB |
Output is correct |
65 |
Correct |
104 ms |
26468 KB |
Output is correct |
66 |
Correct |
105 ms |
26588 KB |
Output is correct |
67 |
Correct |
104 ms |
26596 KB |
Output is correct |
68 |
Correct |
13 ms |
2812 KB |
Output is correct |
69 |
Correct |
10 ms |
2732 KB |
Output is correct |
70 |
Correct |
10 ms |
2732 KB |
Output is correct |
71 |
Correct |
10 ms |
2736 KB |
Output is correct |
72 |
Correct |
10 ms |
2772 KB |
Output is correct |
73 |
Correct |
11 ms |
2816 KB |
Output is correct |
74 |
Correct |
115 ms |
26592 KB |
Output is correct |
75 |
Correct |
117 ms |
26584 KB |
Output is correct |
76 |
Correct |
126 ms |
26660 KB |
Output is correct |
77 |
Correct |
113 ms |
26588 KB |
Output is correct |
78 |
Correct |
107 ms |
26472 KB |
Output is correct |
79 |
Correct |
107 ms |
26572 KB |
Output is correct |
80 |
Correct |
132 ms |
26700 KB |
Output is correct |
81 |
Correct |
107 ms |
26588 KB |
Output is correct |
82 |
Correct |
107 ms |
26700 KB |
Output is correct |
83 |
Correct |
105 ms |
26468 KB |
Output is correct |
84 |
Correct |
109 ms |
26584 KB |
Output is correct |
85 |
Correct |
105 ms |
26592 KB |
Output is correct |
86 |
Correct |
107 ms |
26584 KB |
Output is correct |
87 |
Correct |
108 ms |
26584 KB |
Output is correct |
88 |
Correct |
105 ms |
26472 KB |
Output is correct |
89 |
Correct |
124 ms |
26596 KB |
Output is correct |
90 |
Correct |
116 ms |
26588 KB |
Output is correct |
91 |
Correct |
111 ms |
26580 KB |
Output is correct |
92 |
Correct |
107 ms |
26472 KB |
Output is correct |