#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) {
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 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 |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 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 |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
7 ms |
8172 KB |
Output is correct |
23 |
Correct |
6 ms |
7916 KB |
Output is correct |
24 |
Correct |
5 ms |
5484 KB |
Output is correct |
25 |
Correct |
4 ms |
4588 KB |
Output is correct |
26 |
Correct |
1 ms |
1004 KB |
Output is correct |
27 |
Correct |
1 ms |
748 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 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 |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
7 ms |
8172 KB |
Output is correct |
23 |
Correct |
6 ms |
7916 KB |
Output is correct |
24 |
Correct |
5 ms |
5484 KB |
Output is correct |
25 |
Correct |
4 ms |
4588 KB |
Output is correct |
26 |
Correct |
1 ms |
1004 KB |
Output is correct |
27 |
Correct |
1 ms |
748 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
230 ms |
43492 KB |
Output is correct |
31 |
Correct |
215 ms |
43364 KB |
Output is correct |
32 |
Correct |
151 ms |
43384 KB |
Output is correct |
33 |
Runtime error |
1505 ms |
1048576 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
803 ms |
12000 KB |
Output is correct |
2 |
Correct |
5 ms |
1772 KB |
Output is correct |
3 |
Correct |
244 ms |
5972 KB |
Output is correct |
4 |
Correct |
623 ms |
10104 KB |
Output is correct |
5 |
Correct |
687 ms |
10468 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
42 ms |
5100 KB |
Output is correct |
12 |
Correct |
41 ms |
5612 KB |
Output is correct |
13 |
Correct |
4 ms |
2284 KB |
Output is correct |
14 |
Correct |
2 ms |
1260 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
11 ms |
6124 KB |
Output is correct |
18 |
Correct |
11 ms |
5484 KB |
Output is correct |
19 |
Correct |
6 ms |
2540 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 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 |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
7 ms |
8172 KB |
Output is correct |
23 |
Correct |
6 ms |
7916 KB |
Output is correct |
24 |
Correct |
5 ms |
5484 KB |
Output is correct |
25 |
Correct |
4 ms |
4588 KB |
Output is correct |
26 |
Correct |
1 ms |
1004 KB |
Output is correct |
27 |
Correct |
1 ms |
748 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
230 ms |
43492 KB |
Output is correct |
31 |
Correct |
215 ms |
43364 KB |
Output is correct |
32 |
Correct |
151 ms |
43384 KB |
Output is correct |
33 |
Runtime error |
1505 ms |
1048576 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
34 |
Halted |
0 ms |
0 KB |
- |