#include <bits/stdc++.h>
#include "books.h"
#pragma GCC optimize("Ofast")
#define x first
#define y second
#define ld long double
#define ll long long
using namespace std;
int n, s, x;
map <pair <int, int>, int> calc;
void f(int &l, int &r, vector <int> &L, vector <int> &R, vector <int> &cyc) {
int st = min(l, min(L[cyc[l]], L[cyc[r]])), dr = max(r, max(R[cyc[l]], R[cyc[r]]));
while(st < l || r < dr) {
if(st < l) {
l--;
st = min(st, L[cyc[l]]);
dr = max(dr, R[cyc[l]]);
} else {
r++;
st = min(st, L[cyc[r]]);
dr = max(dr, R[cyc[r]]);
}
}
}
int dp(int l, int r, vector <int> &L, vector <int> &R, vector <int> &cyc) {
f(l, r, L, R, cyc);
if(calc.find({l, r}) != calc.end())
return calc[{l, r}];
int ans = (int)1e9;
int st, dr;
if(l) {
st = l - 1; dr = r;
f(st, dr, L, R, cyc);
ans = 2 + dp(st, dr, L, R, cyc);
}
if(r < cyc.size() - 1) {
st = l; dr = r + 1;
f(st, dr, L, R, cyc);
ans = min(ans, 2 + dp(st, dr, L, R, cyc));
}
calc[{l, r}] = ans;
return ans;
}
ll minimum_walk(vector <int> p, int s) {
ll ans = 0;
int n = p.size(), c = 0, st = s, dr = s;
vector <int> cyc(n + 5, -1), l(n + 5), r(n + 5);
for(int i = 0; i < n; i++) {
ans += abs(i - p[i]);
if(cyc[i] == -1) {
l[c] = r[c] = i;
int j = i;
while(cyc[j] == -1) {
cyc[j] = c;
r[c] = max(r[c], j);
j = p[j];
}
if(i != p[i]) {
st = min(st, l[c]);
dr = max(dr, r[c]);
}
c++;
}
}
calc[{st, dr}] = 0;
return ans + dp(s, s, l, r, cyc);
}
/*int main() {
//ios_base :: sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
cin >> n >> s;
vector <int> p;
for(int i = 1; i <= n; i++)
cin >> x, p.push_back(x);
cout << minimum_walk(p, s);
return 0;
}
*/
Compilation message
books.cpp: In function 'int dp(int, int, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
books.cpp:41:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | if(r < cyc.size() - 1) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
208 ms |
20036 KB |
Output is correct |
31 |
Correct |
203 ms |
19904 KB |
Output is correct |
32 |
Correct |
129 ms |
19940 KB |
Output is correct |
33 |
Correct |
386 ms |
136844 KB |
Output is correct |
34 |
Correct |
377 ms |
136804 KB |
Output is correct |
35 |
Correct |
316 ms |
105060 KB |
Output is correct |
36 |
Correct |
186 ms |
49636 KB |
Output is correct |
37 |
Correct |
137 ms |
27876 KB |
Output is correct |
38 |
Correct |
139 ms |
26848 KB |
Output is correct |
39 |
Correct |
152 ms |
26724 KB |
Output is correct |
40 |
Correct |
138 ms |
26596 KB |
Output is correct |
41 |
Correct |
160 ms |
26724 KB |
Output is correct |
42 |
Correct |
149 ms |
26596 KB |
Output is correct |
43 |
Correct |
139 ms |
26596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
7012 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
16 ms |
2796 KB |
Output is correct |
4 |
Correct |
43 ms |
6628 KB |
Output is correct |
5 |
Correct |
43 ms |
6632 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
876 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
208 ms |
20036 KB |
Output is correct |
31 |
Correct |
203 ms |
19904 KB |
Output is correct |
32 |
Correct |
129 ms |
19940 KB |
Output is correct |
33 |
Correct |
386 ms |
136844 KB |
Output is correct |
34 |
Correct |
377 ms |
136804 KB |
Output is correct |
35 |
Correct |
316 ms |
105060 KB |
Output is correct |
36 |
Correct |
186 ms |
49636 KB |
Output is correct |
37 |
Correct |
137 ms |
27876 KB |
Output is correct |
38 |
Correct |
139 ms |
26848 KB |
Output is correct |
39 |
Correct |
152 ms |
26724 KB |
Output is correct |
40 |
Correct |
138 ms |
26596 KB |
Output is correct |
41 |
Correct |
160 ms |
26724 KB |
Output is correct |
42 |
Correct |
149 ms |
26596 KB |
Output is correct |
43 |
Correct |
139 ms |
26596 KB |
Output is correct |
44 |
Correct |
47 ms |
7012 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
16 ms |
2796 KB |
Output is correct |
47 |
Correct |
43 ms |
6628 KB |
Output is correct |
48 |
Correct |
43 ms |
6632 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
492 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
3 ms |
748 KB |
Output is correct |
55 |
Correct |
3 ms |
876 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
1 ms |
492 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Execution timed out |
2078 ms |
26852 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |