#include <bits/stdc++.h>
#include "books.h"
#define pii pair<int, int>
#define F first
#define S second
using namespace std;
const int MX = 1e6 + 100;
int boss[MX], sz[MX];
int Find(int u) {
return boss[u] == u ? u : boss[u] = Find(boss[u]);
}
void merge(int a, int b) {
a = Find(a), b = Find(b);
if(a == b) return;
boss[a] = b;
sz[b] += sz[a];
}
priority_queue<int> pq[MX];
int G[MX];
long long minimum_walk(vector<int> p, int s) {
int n = p.size();
long long sum = 0, cnt = 0;
for(int i = 0; i < n; i ++) {
boss[i] = i;
sz[i] = 1;
cnt += (i != s and p[i] == i);
G[p[i]] = max(G[p[i]], i);
G[i] = max(G[i], p[i]);
}
for(int i = 0; i < n; i ++) sum += abs(i - p[i]), merge(i, p[i]);
vector<pair<int, pii> > E;
for(int i = 0; i + 1 < n; i ++) {
if(p[i] == i and i != s) continue;
if(Find(i) == Find(i + 1)) continue;
int ptr = i + 1, mx = i;
while(ptr <= G[i] or (ptr != s and ptr < n and p[ptr] == ptr)) {
if(ptr <= G[i]) mx = ptr;
if(p[ptr] == ptr) cnt --;
G[i] = max(G[i], G[ptr]);
merge(i, ptr);
ptr ++;
}
if(ptr < n) E.push_back({ptr - mx, {i, ptr}});
i = ptr - 1;
}
sort(E.begin(), E.end());
for(auto it: E) {
if(Find(it.S.F) != Find(it.S.S)) {
merge(it.S.F, it.S.S);
sum += 2 * it.F;
}
}
assert(sz[Find(s)] + cnt == n);
return sum;
}
//main () {
// cout << minimum_walk({2, 3, 0, 1}, 0);
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
31616 KB |
Output is correct |
2 |
Correct |
30 ms |
31616 KB |
Output is correct |
3 |
Correct |
29 ms |
31744 KB |
Output is correct |
4 |
Correct |
29 ms |
31616 KB |
Output is correct |
5 |
Correct |
29 ms |
31616 KB |
Output is correct |
6 |
Correct |
27 ms |
31616 KB |
Output is correct |
7 |
Correct |
34 ms |
31580 KB |
Output is correct |
8 |
Correct |
29 ms |
31616 KB |
Output is correct |
9 |
Correct |
29 ms |
31744 KB |
Output is correct |
10 |
Correct |
30 ms |
31616 KB |
Output is correct |
11 |
Correct |
31 ms |
31608 KB |
Output is correct |
12 |
Correct |
30 ms |
31616 KB |
Output is correct |
13 |
Correct |
35 ms |
31744 KB |
Output is correct |
14 |
Correct |
28 ms |
31616 KB |
Output is correct |
15 |
Correct |
31 ms |
31608 KB |
Output is correct |
16 |
Correct |
31 ms |
31616 KB |
Output is correct |
17 |
Correct |
36 ms |
31608 KB |
Output is correct |
18 |
Correct |
27 ms |
31608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
31616 KB |
Output is correct |
2 |
Correct |
30 ms |
31616 KB |
Output is correct |
3 |
Correct |
29 ms |
31744 KB |
Output is correct |
4 |
Correct |
29 ms |
31616 KB |
Output is correct |
5 |
Correct |
29 ms |
31616 KB |
Output is correct |
6 |
Correct |
27 ms |
31616 KB |
Output is correct |
7 |
Correct |
34 ms |
31580 KB |
Output is correct |
8 |
Correct |
29 ms |
31616 KB |
Output is correct |
9 |
Correct |
29 ms |
31744 KB |
Output is correct |
10 |
Correct |
30 ms |
31616 KB |
Output is correct |
11 |
Correct |
31 ms |
31608 KB |
Output is correct |
12 |
Correct |
30 ms |
31616 KB |
Output is correct |
13 |
Correct |
35 ms |
31744 KB |
Output is correct |
14 |
Correct |
28 ms |
31616 KB |
Output is correct |
15 |
Correct |
31 ms |
31608 KB |
Output is correct |
16 |
Correct |
31 ms |
31616 KB |
Output is correct |
17 |
Correct |
36 ms |
31608 KB |
Output is correct |
18 |
Correct |
27 ms |
31608 KB |
Output is correct |
19 |
Correct |
34 ms |
31684 KB |
Output is correct |
20 |
Correct |
37 ms |
31736 KB |
Output is correct |
21 |
Correct |
30 ms |
31616 KB |
Output is correct |
22 |
Correct |
35 ms |
31744 KB |
Output is correct |
23 |
Correct |
29 ms |
31608 KB |
Output is correct |
24 |
Correct |
35 ms |
31744 KB |
Output is correct |
25 |
Correct |
33 ms |
31656 KB |
Output is correct |
26 |
Correct |
34 ms |
31616 KB |
Output is correct |
27 |
Correct |
35 ms |
31736 KB |
Output is correct |
28 |
Correct |
29 ms |
31744 KB |
Output is correct |
29 |
Correct |
34 ms |
31616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
31616 KB |
Output is correct |
2 |
Correct |
30 ms |
31616 KB |
Output is correct |
3 |
Correct |
29 ms |
31744 KB |
Output is correct |
4 |
Correct |
29 ms |
31616 KB |
Output is correct |
5 |
Correct |
29 ms |
31616 KB |
Output is correct |
6 |
Correct |
27 ms |
31616 KB |
Output is correct |
7 |
Correct |
34 ms |
31580 KB |
Output is correct |
8 |
Correct |
29 ms |
31616 KB |
Output is correct |
9 |
Correct |
29 ms |
31744 KB |
Output is correct |
10 |
Correct |
30 ms |
31616 KB |
Output is correct |
11 |
Correct |
31 ms |
31608 KB |
Output is correct |
12 |
Correct |
30 ms |
31616 KB |
Output is correct |
13 |
Correct |
35 ms |
31744 KB |
Output is correct |
14 |
Correct |
28 ms |
31616 KB |
Output is correct |
15 |
Correct |
31 ms |
31608 KB |
Output is correct |
16 |
Correct |
31 ms |
31616 KB |
Output is correct |
17 |
Correct |
36 ms |
31608 KB |
Output is correct |
18 |
Correct |
27 ms |
31608 KB |
Output is correct |
19 |
Correct |
34 ms |
31684 KB |
Output is correct |
20 |
Correct |
37 ms |
31736 KB |
Output is correct |
21 |
Correct |
30 ms |
31616 KB |
Output is correct |
22 |
Correct |
35 ms |
31744 KB |
Output is correct |
23 |
Correct |
29 ms |
31608 KB |
Output is correct |
24 |
Correct |
35 ms |
31744 KB |
Output is correct |
25 |
Correct |
33 ms |
31656 KB |
Output is correct |
26 |
Correct |
34 ms |
31616 KB |
Output is correct |
27 |
Correct |
35 ms |
31736 KB |
Output is correct |
28 |
Correct |
29 ms |
31744 KB |
Output is correct |
29 |
Correct |
34 ms |
31616 KB |
Output is correct |
30 |
Correct |
276 ms |
57976 KB |
Output is correct |
31 |
Correct |
346 ms |
58044 KB |
Output is correct |
32 |
Correct |
195 ms |
57976 KB |
Output is correct |
33 |
Incorrect |
230 ms |
61164 KB |
3rd lines differ - on the 1st token, expected: '2121672', found: '2121820' |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
31744 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
31616 KB |
Output is correct |
2 |
Correct |
30 ms |
31616 KB |
Output is correct |
3 |
Correct |
29 ms |
31744 KB |
Output is correct |
4 |
Correct |
29 ms |
31616 KB |
Output is correct |
5 |
Correct |
29 ms |
31616 KB |
Output is correct |
6 |
Correct |
27 ms |
31616 KB |
Output is correct |
7 |
Correct |
34 ms |
31580 KB |
Output is correct |
8 |
Correct |
29 ms |
31616 KB |
Output is correct |
9 |
Correct |
29 ms |
31744 KB |
Output is correct |
10 |
Correct |
30 ms |
31616 KB |
Output is correct |
11 |
Correct |
31 ms |
31608 KB |
Output is correct |
12 |
Correct |
30 ms |
31616 KB |
Output is correct |
13 |
Correct |
35 ms |
31744 KB |
Output is correct |
14 |
Correct |
28 ms |
31616 KB |
Output is correct |
15 |
Correct |
31 ms |
31608 KB |
Output is correct |
16 |
Correct |
31 ms |
31616 KB |
Output is correct |
17 |
Correct |
36 ms |
31608 KB |
Output is correct |
18 |
Correct |
27 ms |
31608 KB |
Output is correct |
19 |
Correct |
34 ms |
31684 KB |
Output is correct |
20 |
Correct |
37 ms |
31736 KB |
Output is correct |
21 |
Correct |
30 ms |
31616 KB |
Output is correct |
22 |
Correct |
35 ms |
31744 KB |
Output is correct |
23 |
Correct |
29 ms |
31608 KB |
Output is correct |
24 |
Correct |
35 ms |
31744 KB |
Output is correct |
25 |
Correct |
33 ms |
31656 KB |
Output is correct |
26 |
Correct |
34 ms |
31616 KB |
Output is correct |
27 |
Correct |
35 ms |
31736 KB |
Output is correct |
28 |
Correct |
29 ms |
31744 KB |
Output is correct |
29 |
Correct |
34 ms |
31616 KB |
Output is correct |
30 |
Correct |
276 ms |
57976 KB |
Output is correct |
31 |
Correct |
346 ms |
58044 KB |
Output is correct |
32 |
Correct |
195 ms |
57976 KB |
Output is correct |
33 |
Incorrect |
230 ms |
61164 KB |
3rd lines differ - on the 1st token, expected: '2121672', found: '2121820' |
34 |
Halted |
0 ms |
0 KB |
- |