#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);
vector<pair<int, int>> st;
for (int i = l; i <= r; i++)
if (to[i] != -1) {
if (!st.size()) st.push_back({ i, to[i] });
else {
if (st.back().second < i) st.push_back({ i, to[i] });
else st.back().second = max(st.back().second, to[i]);
}
}
ll rtn = sum + 2LL * ((int)st.size() - 1);
if (s < l) return rtn + 2 * (l - s);
if (r < s) return rtn + 2 * (s - r);
for (const auto &i : st) {
if (i.first <= s && s <= i.second)
return rtn + 2 * min(s - i.first, i.second - s);
}
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:65:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
480 KB |
Output is correct |
3 |
Correct |
3 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
480 KB |
Output is correct |
7 |
Correct |
2 ms |
480 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
548 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
4 ms |
548 KB |
Output is correct |
13 |
Correct |
3 ms |
548 KB |
Output is correct |
14 |
Correct |
5 ms |
548 KB |
Output is correct |
15 |
Correct |
3 ms |
548 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 |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
480 KB |
Output is correct |
3 |
Correct |
3 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
480 KB |
Output is correct |
7 |
Correct |
2 ms |
480 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
548 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
4 ms |
548 KB |
Output is correct |
13 |
Correct |
3 ms |
548 KB |
Output is correct |
14 |
Correct |
5 ms |
548 KB |
Output is correct |
15 |
Correct |
3 ms |
548 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 |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 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 |
3 ms |
736 KB |
Output is correct |
28 |
Correct |
2 ms |
736 KB |
Output is correct |
29 |
Correct |
3 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
480 KB |
Output is correct |
3 |
Correct |
3 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
480 KB |
Output is correct |
7 |
Correct |
2 ms |
480 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
548 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
4 ms |
548 KB |
Output is correct |
13 |
Correct |
3 ms |
548 KB |
Output is correct |
14 |
Correct |
5 ms |
548 KB |
Output is correct |
15 |
Correct |
3 ms |
548 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 |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 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 |
3 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 |
298 ms |
20160 KB |
Output is correct |
31 |
Correct |
355 ms |
20220 KB |
Output is correct |
32 |
Correct |
140 ms |
20220 KB |
Output is correct |
33 |
Correct |
199 ms |
28588 KB |
Output is correct |
34 |
Correct |
188 ms |
28588 KB |
Output is correct |
35 |
Correct |
212 ms |
28588 KB |
Output is correct |
36 |
Correct |
183 ms |
28588 KB |
Output is correct |
37 |
Correct |
184 ms |
28588 KB |
Output is correct |
38 |
Correct |
188 ms |
28588 KB |
Output is correct |
39 |
Correct |
244 ms |
28588 KB |
Output is correct |
40 |
Correct |
208 ms |
28588 KB |
Output is correct |
41 |
Correct |
357 ms |
28588 KB |
Output is correct |
42 |
Correct |
266 ms |
28588 KB |
Output is correct |
43 |
Correct |
178 ms |
28588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
28588 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
480 KB |
Output is correct |
3 |
Correct |
3 ms |
480 KB |
Output is correct |
4 |
Correct |
2 ms |
480 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
480 KB |
Output is correct |
7 |
Correct |
2 ms |
480 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
548 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
4 ms |
548 KB |
Output is correct |
13 |
Correct |
3 ms |
548 KB |
Output is correct |
14 |
Correct |
5 ms |
548 KB |
Output is correct |
15 |
Correct |
3 ms |
548 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 |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 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 |
3 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 |
298 ms |
20160 KB |
Output is correct |
31 |
Correct |
355 ms |
20220 KB |
Output is correct |
32 |
Correct |
140 ms |
20220 KB |
Output is correct |
33 |
Correct |
199 ms |
28588 KB |
Output is correct |
34 |
Correct |
188 ms |
28588 KB |
Output is correct |
35 |
Correct |
212 ms |
28588 KB |
Output is correct |
36 |
Correct |
183 ms |
28588 KB |
Output is correct |
37 |
Correct |
184 ms |
28588 KB |
Output is correct |
38 |
Correct |
188 ms |
28588 KB |
Output is correct |
39 |
Correct |
244 ms |
28588 KB |
Output is correct |
40 |
Correct |
208 ms |
28588 KB |
Output is correct |
41 |
Correct |
357 ms |
28588 KB |
Output is correct |
42 |
Correct |
266 ms |
28588 KB |
Output is correct |
43 |
Correct |
178 ms |
28588 KB |
Output is correct |
44 |
Incorrect |
2 ms |
28588 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3594' |
45 |
Halted |
0 ms |
0 KB |
- |