#include "books.h"
#include <bits/stdc++.h>
#define endl '\n'
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7, inf = 1e9 + 7;
const ll hs = 199;
const ldb eps = 1e-9, pi = acos(-1);
using namespace std;
int n;
vector<int> a, vis, to;
int cycle(int st) {
vis[st] = 1;
int rtn = st;
st = a[st];
while (!vis[st]) {
rtn = max(rtn, st);
vis[st] = 1;
st = a[st];
}
return rtn;
}
ll minimum_walk(vector<int> p, int s) {
n = p.size();
a = p;
ll sum = 0;
for (int i = 0; i < n; i++)
sum += abs(i - a[i]);
int l = 0, r = n - 1;
while (l < n && a[l] == l) l++;
if (l == n) return 0;
while (a[r] == r) r--;
vis.resize(n, 0);
to.resize(n, -1);
for (int i = l; i <= r; i++)
if (!vis[i])
to[i] = cycle(i);
ll cnt = 0, mxpos = 0;
for (int i = l; i <= r; i++)
if (to[i] != -1) {
if (i < mxpos) mxpos = max(mxpos, (ll)to[i]);
else mxpos = to[i], cnt++;
}
ll rtn = sum + 2LL * (cnt - 1);
if (s < l) rtn += 2 * (l - s);
if (r < s) rtn += 2 * (s - r);
return rtn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
2 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
520 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
2 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
520 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
3 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
736 KB |
Output is correct |
25 |
Correct |
2 ms |
736 KB |
Output is correct |
26 |
Correct |
2 ms |
736 KB |
Output is correct |
27 |
Correct |
2 ms |
736 KB |
Output is correct |
28 |
Correct |
2 ms |
736 KB |
Output is correct |
29 |
Correct |
3 ms |
736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
2 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
520 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
3 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
736 KB |
Output is correct |
25 |
Correct |
2 ms |
736 KB |
Output is correct |
26 |
Correct |
2 ms |
736 KB |
Output is correct |
27 |
Correct |
2 ms |
736 KB |
Output is correct |
28 |
Correct |
2 ms |
736 KB |
Output is correct |
29 |
Correct |
3 ms |
736 KB |
Output is correct |
30 |
Correct |
263 ms |
20204 KB |
Output is correct |
31 |
Correct |
268 ms |
20204 KB |
Output is correct |
32 |
Correct |
143 ms |
20204 KB |
Output is correct |
33 |
Correct |
174 ms |
20204 KB |
Output is correct |
34 |
Correct |
200 ms |
20204 KB |
Output is correct |
35 |
Correct |
169 ms |
20204 KB |
Output is correct |
36 |
Correct |
170 ms |
20204 KB |
Output is correct |
37 |
Correct |
169 ms |
20204 KB |
Output is correct |
38 |
Correct |
188 ms |
20204 KB |
Output is correct |
39 |
Correct |
181 ms |
20204 KB |
Output is correct |
40 |
Correct |
235 ms |
20204 KB |
Output is correct |
41 |
Correct |
215 ms |
20204 KB |
Output is correct |
42 |
Correct |
237 ms |
20220 KB |
Output is correct |
43 |
Correct |
160 ms |
20220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
20220 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
6 |
Correct |
3 ms |
520 KB |
Output is correct |
7 |
Correct |
2 ms |
520 KB |
Output is correct |
8 |
Correct |
2 ms |
520 KB |
Output is correct |
9 |
Correct |
2 ms |
520 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
3 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
620 KB |
Output is correct |
23 |
Correct |
3 ms |
620 KB |
Output is correct |
24 |
Correct |
2 ms |
736 KB |
Output is correct |
25 |
Correct |
2 ms |
736 KB |
Output is correct |
26 |
Correct |
2 ms |
736 KB |
Output is correct |
27 |
Correct |
2 ms |
736 KB |
Output is correct |
28 |
Correct |
2 ms |
736 KB |
Output is correct |
29 |
Correct |
3 ms |
736 KB |
Output is correct |
30 |
Correct |
263 ms |
20204 KB |
Output is correct |
31 |
Correct |
268 ms |
20204 KB |
Output is correct |
32 |
Correct |
143 ms |
20204 KB |
Output is correct |
33 |
Correct |
174 ms |
20204 KB |
Output is correct |
34 |
Correct |
200 ms |
20204 KB |
Output is correct |
35 |
Correct |
169 ms |
20204 KB |
Output is correct |
36 |
Correct |
170 ms |
20204 KB |
Output is correct |
37 |
Correct |
169 ms |
20204 KB |
Output is correct |
38 |
Correct |
188 ms |
20204 KB |
Output is correct |
39 |
Correct |
181 ms |
20204 KB |
Output is correct |
40 |
Correct |
235 ms |
20204 KB |
Output is correct |
41 |
Correct |
215 ms |
20204 KB |
Output is correct |
42 |
Correct |
237 ms |
20220 KB |
Output is correct |
43 |
Correct |
160 ms |
20220 KB |
Output is correct |
44 |
Incorrect |
2 ms |
20220 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |