#include "books.h"
#ifdef NYAOWO
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
#define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;
LL minimum_walk(vector<i32> p, i32 s) {
assert(s >= 0);
int n = sz(p);
int ans = 0;
vector<int> cid(n, -1);
vector<pii> range;
For(i, 0, n - 1) if(p[i] != i && cid[i] == -1) {
int cur = i;
int l = n + 1;
int r = -1;
do {
l = min(l, cur);
r = max(r, cur);
ans += abs(p[cur] - cur);
cur = p[cur];
cid[cur] = sz(range);
} while(cur != i);
range.eb(l, r);
}
if(sz(range) == 0) return 0;
vector<int> nxtr(n), nxtl(n);
nxtl[0] = -1e10;
For(i, 1, n - 1) {
if(cid[i - 1] >= 0) nxtl[i] = i - 1;
else nxtl[i] = nxtl[i - 1];
}
nxtr[n - 1] = 1e10;
Forr(i, n - 2, 0) {
if(cid[i + 1] >= 0) nxtr[i] = i + 1;
else nxtr[i] = nxtr[i + 1];
}
int ll = 0, rr = n - 1;
while(p[ll] == ll) ll++;
while(p[rr] == rr) rr--;
ll = min(ll, (int)s);
rr = max(rr, (int)s);
int l, r;
l = s; r = s;
queue<int> que;
if(cid[s] >= 0) que.emplace(cid[s]);
while(l > ll || r < rr) {
pii rg;
if(!que.empty()) {
int id = que.front(); que.pop();
rg = range[id];
} else {
int cl = l - nxtl[l];
int cr = nxtr[r] - r;
if(cl < cr) {
ans += cl * 2;
rg = pii(nxtl[l], r);
} else {
ans += cr * 2;
rg = pii(l, nxtr[r]);
}
}
while(l > rg.F) {
l--;
if(cid[l] >= 0) que.emplace(cid[l]);
}
while(r < rg.S) {
r++;
if(cid[r] >= 0) que.emplace(cid[r]);
}
}
return ans;
}
# |
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 |
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 |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
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 |
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 |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 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 |
340 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 |
1 ms |
340 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 |
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 |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 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 |
340 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 |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
117 ms |
39804 KB |
Output is correct |
31 |
Correct |
124 ms |
39804 KB |
Output is correct |
32 |
Correct |
80 ms |
15860 KB |
Output is correct |
33 |
Correct |
120 ms |
34648 KB |
Output is correct |
34 |
Correct |
107 ms |
34744 KB |
Output is correct |
35 |
Correct |
110 ms |
35252 KB |
Output is correct |
36 |
Correct |
107 ms |
34188 KB |
Output is correct |
37 |
Correct |
141 ms |
32108 KB |
Output is correct |
38 |
Correct |
96 ms |
31728 KB |
Output is correct |
39 |
Correct |
99 ms |
31724 KB |
Output is correct |
40 |
Correct |
100 ms |
31996 KB |
Output is correct |
41 |
Correct |
109 ms |
35692 KB |
Output is correct |
42 |
Correct |
110 ms |
34808 KB |
Output is correct |
43 |
Correct |
118 ms |
39644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
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 |
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 |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 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 |
340 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 |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
117 ms |
39804 KB |
Output is correct |
31 |
Correct |
124 ms |
39804 KB |
Output is correct |
32 |
Correct |
80 ms |
15860 KB |
Output is correct |
33 |
Correct |
120 ms |
34648 KB |
Output is correct |
34 |
Correct |
107 ms |
34744 KB |
Output is correct |
35 |
Correct |
110 ms |
35252 KB |
Output is correct |
36 |
Correct |
107 ms |
34188 KB |
Output is correct |
37 |
Correct |
141 ms |
32108 KB |
Output is correct |
38 |
Correct |
96 ms |
31728 KB |
Output is correct |
39 |
Correct |
99 ms |
31724 KB |
Output is correct |
40 |
Correct |
100 ms |
31996 KB |
Output is correct |
41 |
Correct |
109 ms |
35692 KB |
Output is correct |
42 |
Correct |
110 ms |
34808 KB |
Output is correct |
43 |
Correct |
118 ms |
39644 KB |
Output is correct |
44 |
Incorrect |
0 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3324' |
45 |
Halted |
0 ms |
0 KB |
- |