#include "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<ll> rands;
vector<int> dist;
map<ll, int> ma;
long long minimum_walk(vector<int> p, int s) {
srand(time(NULL));
int n = p.size();
rands.resize(n, 0);
for(int i = 0; i < n; i++) {
for(int j = 0; j < 4; j++) {
rands[i] *= 32768;
rands[i] += (rand() % 32768);
}
}
ll tot = 0;
vector<bool> taken(n, false);
vector<int> nums(n, -1);
for(int i = 0; i < n; i++) {
if(!taken[i]) {
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);
}
if(l != r) {
nums[l] = nums[r] = l;
}
}
}
ll hash = 0;
int lastZero = 0;
int lastImpt = -999999;
bool foundC = false;
int l1 = s-1, r1 = s;
for(int i = 0; i < n; i++) {
if(nums[i] == -1) {
if(hash == 0) tot += 2;
continue;
}
lastImpt = i;
hash ^= rands[nums[i]];
if(hash) {
if(ma.count(hash)) {
int l = ma[hash], r = i;
if(l <= l1 && r1 <= r) {
tot += 2 * min(r - r1, l1 - l);
}
ma.erase(hash);
}
else ma[hash] = i;
}
else {
if(i != lastZero) tot += 2;
lastZero = i + 1;
}
}
for(int i = 0; i < n; i++) {
if(p[i] == i && i != s) tot -= 2;
else break;
}
for(int i = n-1; i >= 0; i--) {
if(p[i] == i && i != s) tot -= 2;
else break;
}
return max(0LL, tot - 2);
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:42:9: warning: variable 'lastImpt' set but not used [-Wunused-but-set-variable]
42 | int lastImpt = -999999;
| ^~~~~~~~
books.cpp:43:10: warning: unused variable 'foundC' [-Wunused-variable]
43 | bool foundC = false;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
224 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
224 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 |
161 ms |
24036 KB |
Output is correct |
31 |
Correct |
152 ms |
23992 KB |
Output is correct |
32 |
Correct |
137 ms |
24152 KB |
Output is correct |
33 |
Correct |
299 ms |
36368 KB |
Output is correct |
34 |
Correct |
259 ms |
36188 KB |
Output is correct |
35 |
Correct |
327 ms |
37812 KB |
Output is correct |
36 |
Correct |
269 ms |
34484 KB |
Output is correct |
37 |
Correct |
153 ms |
26060 KB |
Output is correct |
38 |
Correct |
138 ms |
24552 KB |
Output is correct |
39 |
Correct |
129 ms |
24424 KB |
Output is correct |
40 |
Correct |
136 ms |
24156 KB |
Output is correct |
41 |
Correct |
141 ms |
24012 KB |
Output is correct |
42 |
Correct |
136 ms |
24100 KB |
Output is correct |
43 |
Correct |
283 ms |
26636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2748' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 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 |
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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
224 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 |
161 ms |
24036 KB |
Output is correct |
31 |
Correct |
152 ms |
23992 KB |
Output is correct |
32 |
Correct |
137 ms |
24152 KB |
Output is correct |
33 |
Correct |
299 ms |
36368 KB |
Output is correct |
34 |
Correct |
259 ms |
36188 KB |
Output is correct |
35 |
Correct |
327 ms |
37812 KB |
Output is correct |
36 |
Correct |
269 ms |
34484 KB |
Output is correct |
37 |
Correct |
153 ms |
26060 KB |
Output is correct |
38 |
Correct |
138 ms |
24552 KB |
Output is correct |
39 |
Correct |
129 ms |
24424 KB |
Output is correct |
40 |
Correct |
136 ms |
24156 KB |
Output is correct |
41 |
Correct |
141 ms |
24012 KB |
Output is correct |
42 |
Correct |
136 ms |
24100 KB |
Output is correct |
43 |
Correct |
283 ms |
26636 KB |
Output is correct |
44 |
Incorrect |
1 ms |
212 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2748' |
45 |
Halted |
0 ms |
0 KB |
- |