#include "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<vector<int>> adj;
vector<int> dist;
long long minimum_walk(vector<int> p, int s) {
ll tot = 0;
int n = p.size();
vector<bool> taken(n, false);
int g = n;
adj.resize(n);
for(int i = 1; i < n; i++) {
adj[i].push_back(i-1);
adj[i-1].push_back(i);
}
for(int i = 0; i < n; i++) {
if(!taken[i]) {
adj.push_back(vector<int>());
int x = i;
int l = i, r = i;
while(!taken[x]) {
taken[x] = true;
tot += abs(x - p[x]);
x = p[x];
l = min(l, x);
r = max(r, x);
adj[x].push_back(g);
}
for(int j = l; j <= r; j++) {
adj[g].push_back(j);
}
g++;
}
}
dist.resize(g, 1e9 + 7);
int best = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
while(pq.size()) {
auto pa = pq.top();
pq.pop();
int len = pa.first, x = pa.second;
if(len > dist[x]) continue;
if(x < n) {
if(p[x] != x) best = max(best, len);
}
for(int ch : adj[x]) {
int goLength;
if(max(ch, x) < n) {
goLength = len + 1;
}
else {
goLength = len;
}
if(dist[ch] <= goLength) continue;
dist[ch] = goLength;
pq.push({goLength, ch});
}
}
return tot + 2 * best;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
428 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
428 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
428 KB |
Output is correct |
30 |
Correct |
981 ms |
114320 KB |
Output is correct |
31 |
Correct |
1060 ms |
128652 KB |
Output is correct |
32 |
Correct |
310 ms |
132328 KB |
Output is correct |
33 |
Correct |
315 ms |
114632 KB |
Output is correct |
34 |
Correct |
368 ms |
114748 KB |
Output is correct |
35 |
Correct |
310 ms |
107284 KB |
Output is correct |
36 |
Correct |
319 ms |
95776 KB |
Output is correct |
37 |
Correct |
412 ms |
98228 KB |
Output is correct |
38 |
Correct |
451 ms |
105896 KB |
Output is correct |
39 |
Correct |
475 ms |
105900 KB |
Output is correct |
40 |
Correct |
543 ms |
109260 KB |
Output is correct |
41 |
Correct |
764 ms |
120080 KB |
Output is correct |
42 |
Correct |
713 ms |
124696 KB |
Output is correct |
43 |
Correct |
815 ms |
231308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
432 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
300 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1592' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
428 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
428 KB |
Output is correct |
30 |
Correct |
981 ms |
114320 KB |
Output is correct |
31 |
Correct |
1060 ms |
128652 KB |
Output is correct |
32 |
Correct |
310 ms |
132328 KB |
Output is correct |
33 |
Correct |
315 ms |
114632 KB |
Output is correct |
34 |
Correct |
368 ms |
114748 KB |
Output is correct |
35 |
Correct |
310 ms |
107284 KB |
Output is correct |
36 |
Correct |
319 ms |
95776 KB |
Output is correct |
37 |
Correct |
412 ms |
98228 KB |
Output is correct |
38 |
Correct |
451 ms |
105896 KB |
Output is correct |
39 |
Correct |
475 ms |
105900 KB |
Output is correct |
40 |
Correct |
543 ms |
109260 KB |
Output is correct |
41 |
Correct |
764 ms |
120080 KB |
Output is correct |
42 |
Correct |
713 ms |
124696 KB |
Output is correct |
43 |
Correct |
815 ms |
231308 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
432 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Incorrect |
1 ms |
300 KB |
3rd lines differ - on the 1st token, expected: '2094', found: '1592' |
48 |
Halted |
0 ms |
0 KB |
- |