#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6 + 5, INF = 1e9;
int p[maxn], vis[maxn];
vector<pair<int,int>> vec = {{-1, -1}};
int minimum_walk(vector<int32_t> P, int32_t s) {
int n = P.size();
for (int i=0;i<n;i++) p[i] = P[i];
for (int i=0;i<n;i++) vis[i] = 0;
int ans = 0, cnt = 0;
for (int i=0;i<n;i++) if (!vis[i]) {
if (p[i]==i && i==s) {
cnt++;
vis[i] = cnt;
vec.push_back({s, s});
}
if (p[i]==i) continue;
int u = i;
pair<int,int> val = {u, u};
cnt++;
while (true) {
vis[u] = cnt;
val.first = min(u, val.first);
val.second = max(u, val.second);
int v = p[u];
ans += abs(v-u);
if (v==i) break;
u = v;
}
vec.push_back(val);
}
sort(vec.begin(), vec.end());
int lastl = vec[1].first, mx = vec[1].second;
int LL = -1, RR = -1;
for (int i=2;i<vec.size();i++) {
if (vec[i].first > mx) {
if (lastl<=s && s<=mx) {
LL = lastl, RR = mx;
}
lastl = vec[i].first;
ans += (vec[i].first - mx)*2;
}
mx = max(vec[i].second, mx);
}
if (LL == -1) LL = lastl, RR = mx;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0, vis[s]});
int dist[cnt+5];
for (int i=1;i<=cnt;i++) dist[i] = -1;
while (pq.size() > 0) {
int u = pq.top().second, d = pq.top().first;
pq.pop();
if (vec[u].second < LL || vec[u].first > RR) dist[u] = -2;
if (dist[u]!=-1) continue;
dist[u] = d;
for (int i=vec[u].first; i<=vec[u].second;i++) {
if (p[i]!=i && dist[vis[i]]==-1) pq.push({d, vis[i]});
}
for (int i=vec[u].first-1;i>=LL;i--) {
if (p[i]!=i) {
if (dist[vis[i]]==-1) pq.push({d + (vec[u].first - i)*2, vis[i]});
}
}
for (int i=vec[u].second+1;i<=RR;i++) {
if (p[i]!=i) {
if (dist[vis[i]]==-1) pq.push({d + (i - vec[u].first)*2, vis[i]});
}
}
}
for (int i=1;i<=cnt;i++) assert((dist[i]==-1) == (vec[i].first>RR || vec[i].second<LL));
int MX = 0;
for (int i=1;i<=cnt;i++) if (dist[i]!=-1) MX = max(dist[i], MX);
ans += MX;
return ans;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int32_t)':
books.cpp:39:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i=2;i<vec.size();i++) {
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
7 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
7 ms |
3536 KB |
Output is correct |
30 |
Correct |
137 ms |
28608 KB |
Output is correct |
31 |
Correct |
212 ms |
41912 KB |
Output is correct |
32 |
Correct |
63 ms |
23804 KB |
Output is correct |
33 |
Correct |
77 ms |
28524 KB |
Output is correct |
34 |
Correct |
71 ms |
28352 KB |
Output is correct |
35 |
Correct |
74 ms |
29288 KB |
Output is correct |
36 |
Correct |
79 ms |
29892 KB |
Output is correct |
37 |
Correct |
66 ms |
24524 KB |
Output is correct |
38 |
Correct |
68 ms |
24060 KB |
Output is correct |
39 |
Correct |
67 ms |
24144 KB |
Output is correct |
40 |
Correct |
70 ms |
24148 KB |
Output is correct |
41 |
Correct |
86 ms |
24456 KB |
Output is correct |
42 |
Correct |
81 ms |
23816 KB |
Output is correct |
43 |
Runtime error |
953 ms |
1048576 KB |
Execution killed with signal 9 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3028 KB |
Output is correct |
2 |
Correct |
5 ms |
3032 KB |
Output is correct |
3 |
Correct |
7 ms |
3540 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
7 ms |
3540 KB |
Output is correct |
7 |
Correct |
8 ms |
3604 KB |
Output is correct |
8 |
Incorrect |
14 ms |
4556 KB |
3rd lines differ - on the 1st token, expected: '10478', found: '10504' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2392 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
0 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
7 ms |
3536 KB |
Output is correct |
30 |
Correct |
137 ms |
28608 KB |
Output is correct |
31 |
Correct |
212 ms |
41912 KB |
Output is correct |
32 |
Correct |
63 ms |
23804 KB |
Output is correct |
33 |
Correct |
77 ms |
28524 KB |
Output is correct |
34 |
Correct |
71 ms |
28352 KB |
Output is correct |
35 |
Correct |
74 ms |
29288 KB |
Output is correct |
36 |
Correct |
79 ms |
29892 KB |
Output is correct |
37 |
Correct |
66 ms |
24524 KB |
Output is correct |
38 |
Correct |
68 ms |
24060 KB |
Output is correct |
39 |
Correct |
67 ms |
24144 KB |
Output is correct |
40 |
Correct |
70 ms |
24148 KB |
Output is correct |
41 |
Correct |
86 ms |
24456 KB |
Output is correct |
42 |
Correct |
81 ms |
23816 KB |
Output is correct |
43 |
Runtime error |
953 ms |
1048576 KB |
Execution killed with signal 9 |
44 |
Halted |
0 ms |
0 KB |
- |