#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]);
nxtR = max(nxtR, cycleR[curL]);
}
else {
curR++;
nxtL = min(nxtL, cycleL[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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
272 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
272 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
272 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
119 ms |
15876 KB |
Output is correct |
31 |
Correct |
134 ms |
15856 KB |
Output is correct |
32 |
Correct |
75 ms |
15872 KB |
Output is correct |
33 |
Correct |
85 ms |
15868 KB |
Output is correct |
34 |
Correct |
88 ms |
15948 KB |
Output is correct |
35 |
Correct |
86 ms |
15944 KB |
Output is correct |
36 |
Correct |
84 ms |
15860 KB |
Output is correct |
37 |
Correct |
80 ms |
15860 KB |
Output is correct |
38 |
Correct |
82 ms |
15860 KB |
Output is correct |
39 |
Correct |
81 ms |
15952 KB |
Output is correct |
40 |
Correct |
88 ms |
15944 KB |
Output is correct |
41 |
Correct |
110 ms |
15860 KB |
Output is correct |
42 |
Correct |
100 ms |
15860 KB |
Output is correct |
43 |
Correct |
126 ms |
15872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
Incorrect |
1 ms |
308 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22130' |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
272 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
119 ms |
15876 KB |
Output is correct |
31 |
Correct |
134 ms |
15856 KB |
Output is correct |
32 |
Correct |
75 ms |
15872 KB |
Output is correct |
33 |
Correct |
85 ms |
15868 KB |
Output is correct |
34 |
Correct |
88 ms |
15948 KB |
Output is correct |
35 |
Correct |
86 ms |
15944 KB |
Output is correct |
36 |
Correct |
84 ms |
15860 KB |
Output is correct |
37 |
Correct |
80 ms |
15860 KB |
Output is correct |
38 |
Correct |
82 ms |
15860 KB |
Output is correct |
39 |
Correct |
81 ms |
15952 KB |
Output is correct |
40 |
Correct |
88 ms |
15944 KB |
Output is correct |
41 |
Correct |
110 ms |
15860 KB |
Output is correct |
42 |
Correct |
100 ms |
15860 KB |
Output is correct |
43 |
Correct |
126 ms |
15872 KB |
Output is correct |
44 |
Correct |
0 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 |
Incorrect |
1 ms |
308 KB |
3rd lines differ - on the 1st token, expected: '22140', found: '22130' |
50 |
Halted |
0 ms |
0 KB |
- |