#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
using ll = long long int;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int inf = 1.05e9;
const ll INF = 1e18;
ll minimum_walk(vector<int> p, int s) {
ll ans = 0, mx = 0;
int n = p.size(), l = s, r = s;
ll d[n], g[n];
memset(d, -1, sizeof d);
memset(g, -1, sizeof g);
d[s] = 0;
queue<int> q[2];
q[0].push(s);
while(true){
while(!q[0].empty()){
int v = q[0].front();
q[0].pop();
for(int i = p[v]; d[i] == -1; i = p[i]){
d[i] = d[v];
q[0].push(i);
}
while(l > v){
l--;
if(d[l] == -1){
d[l] = d[v];
q[0].push(l);
}
}
while(r < v){
r++;
if(d[r] == -1){
d[r] = d[v];
q[0].push(r);
}
}
if(v && g[v - 1] == -1){
g[v - 1] = d[v] + 1;
q[1].push(v - 1);
}
if(v + 1 < n && g[v + 1] == -1){
g[v + 1] = d[v] + 1;
q[1].push(v + 1);
}
}
if(q[1].empty()) break;
int v = q[1].front();
q[1].pop();
if(d[v] != -1) continue;
d[v] = g[v];
for(int i = p[v]; d[i] == -1; i = p[i]){
d[i] = d[v];
q[0].push(i);
}
while(l > v){
l--;
if(d[l] == -1){
d[l] = d[v];
q[0].push(l);
}
}
while(r < v){
r++;
if(d[r] == -1){
d[r] = d[v];
q[0].push(r);
}
}
if(v && g[v - 1] == -1){
g[v - 1] = d[v] + 1;
q[1].push(v - 1);
}
if(v + 1 < n && g[v + 1] == -1){
g[v + 1] = d[v] + 1;
q[1].push(v + 1);
}
}
for(int i = 0; i < n; i++){
ans += abs(i - p[i]);
if(p[i] != i) mx = max(mx, d[i]);
}
return ans + 2 * mx;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
228 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
228 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
202 ms |
35692 KB |
Output is correct |
31 |
Correct |
185 ms |
35696 KB |
Output is correct |
32 |
Correct |
116 ms |
30444 KB |
Output is correct |
33 |
Correct |
134 ms |
30440 KB |
Output is correct |
34 |
Correct |
125 ms |
30428 KB |
Output is correct |
35 |
Correct |
147 ms |
30552 KB |
Output is correct |
36 |
Correct |
162 ms |
30444 KB |
Output is correct |
37 |
Correct |
129 ms |
30448 KB |
Output is correct |
38 |
Correct |
132 ms |
30444 KB |
Output is correct |
39 |
Correct |
135 ms |
30564 KB |
Output is correct |
40 |
Correct |
139 ms |
30764 KB |
Output is correct |
41 |
Correct |
159 ms |
33124 KB |
Output is correct |
42 |
Correct |
147 ms |
32460 KB |
Output is correct |
43 |
Correct |
130 ms |
34808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1592' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
228 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
202 ms |
35692 KB |
Output is correct |
31 |
Correct |
185 ms |
35696 KB |
Output is correct |
32 |
Correct |
116 ms |
30444 KB |
Output is correct |
33 |
Correct |
134 ms |
30440 KB |
Output is correct |
34 |
Correct |
125 ms |
30428 KB |
Output is correct |
35 |
Correct |
147 ms |
30552 KB |
Output is correct |
36 |
Correct |
162 ms |
30444 KB |
Output is correct |
37 |
Correct |
129 ms |
30448 KB |
Output is correct |
38 |
Correct |
132 ms |
30444 KB |
Output is correct |
39 |
Correct |
135 ms |
30564 KB |
Output is correct |
40 |
Correct |
139 ms |
30764 KB |
Output is correct |
41 |
Correct |
159 ms |
33124 KB |
Output is correct |
42 |
Correct |
147 ms |
32460 KB |
Output is correct |
43 |
Correct |
130 ms |
34808 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1592' |
48 |
Halted |
0 ms |
0 KB |
- |