#include "books.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 20;
int cycleL[maxN];
int cycleR[maxN];
void extend(int &curL, int &curR) {
int nxtL = min(curL, cycleL[curL]);
int nxtR = max(curR, cycleR[curR]);
while (curL > nxtL || curR < nxtR) {
if (curL > nxtL) {
curL--;
nxtL = min(nxtL, cycleL[curL]);
}
else {
curR++;
nxtR = max(nxtR, cycleR[curR]);
}
}
}
long long minimum_walk(vector<int> A, int start) {
int N = A.size();
for (int i = 0; i < N; i++) {
cycleL[i] = -1;
cycleR[i] = -1;
}
int curL = start;
int curR = start;
int endL = start;
int endR = start;
long long res = 0;
for (int i = 0; i < N; i++) {
if (i != A[i]) {
endL = min(endL, i);
endR = max(endR, i);
}
res += abs(A[i] - i);
if (cycleL[i] == -1) {
int L = i;
int R = i;
int cur = i;
do {
L = min(L, cur);
R = max(R, cur);
cur = A[cur];
}
while (cur != i);
cur = i;
do {
cycleL[cur] = L;
cycleR[cur] = R;
cur = A[cur];
}
while (cur != i);
}
}
while (curL > endL || curR < endR) {
extend(curL, curR);
int costL = 0;
int nxtLL = curL;
int nxtLR = curR;
while (nxtLL > endL && nxtLR == curR) {
nxtLL--;
costL += 2;
extend(nxtLL, nxtLR);
}
int costR = 0;
int nxtRL = curL;
int nxtRR = curR;
while (nxtRR < endR && nxtRL == curL) {
nxtRR++;
costR += 2;
extend(nxtRL, nxtRR);
}
if (nxtLR == curR && nxtRL == curL) {
res += costL + costR;
}
else {
res += min(costL, costR);
}
curL = min(nxtLL, nxtRL);
curR = max(nxtLR, nxtRR);
}
return res;
}
# |
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 |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
0 ms |
300 KB |
Output is correct |
17 |
Correct |
0 ms |
308 KB |
Output is correct |
18 |
Correct |
0 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 |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
0 ms |
300 KB |
Output is correct |
17 |
Correct |
0 ms |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
244 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 |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
312 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 |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
0 ms |
300 KB |
Output is correct |
17 |
Correct |
0 ms |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
244 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 |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
133 ms |
22676 KB |
Output is correct |
31 |
Correct |
139 ms |
22672 KB |
Output is correct |
32 |
Correct |
79 ms |
22640 KB |
Output is correct |
33 |
Correct |
85 ms |
22676 KB |
Output is correct |
34 |
Correct |
105 ms |
22628 KB |
Output is correct |
35 |
Correct |
89 ms |
22608 KB |
Output is correct |
36 |
Correct |
83 ms |
22672 KB |
Output is correct |
37 |
Correct |
81 ms |
22624 KB |
Output is correct |
38 |
Correct |
90 ms |
22692 KB |
Output is correct |
39 |
Correct |
81 ms |
22604 KB |
Output is correct |
40 |
Correct |
86 ms |
22604 KB |
Output is correct |
41 |
Correct |
118 ms |
22604 KB |
Output is correct |
42 |
Correct |
99 ms |
22676 KB |
Output is correct |
43 |
Correct |
83 ms |
22760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4070' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
308 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 |
304 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
0 ms |
300 KB |
Output is correct |
17 |
Correct |
0 ms |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
244 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 |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
304 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
133 ms |
22676 KB |
Output is correct |
31 |
Correct |
139 ms |
22672 KB |
Output is correct |
32 |
Correct |
79 ms |
22640 KB |
Output is correct |
33 |
Correct |
85 ms |
22676 KB |
Output is correct |
34 |
Correct |
105 ms |
22628 KB |
Output is correct |
35 |
Correct |
89 ms |
22608 KB |
Output is correct |
36 |
Correct |
83 ms |
22672 KB |
Output is correct |
37 |
Correct |
81 ms |
22624 KB |
Output is correct |
38 |
Correct |
90 ms |
22692 KB |
Output is correct |
39 |
Correct |
81 ms |
22604 KB |
Output is correct |
40 |
Correct |
86 ms |
22604 KB |
Output is correct |
41 |
Correct |
118 ms |
22604 KB |
Output is correct |
42 |
Correct |
99 ms |
22676 KB |
Output is correct |
43 |
Correct |
83 ms |
22760 KB |
Output is correct |
44 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '4070' |
45 |
Halted |
0 ms |
0 KB |
- |