#include "books.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 20;
int par[maxn];
int depth[maxn];
bool flag[maxn];
bool skip[maxn];
int nxt[maxn];
int pref[maxn];
int root(int u) {
if (par[u] == -1) {
return u;
}
else {
return par[u] = root(par[u]);
}
}
bool update(int u, int v) {
int ru = root(u);
int rv = root(v);
if (ru == rv) {
return false;
}
if (depth[ru] > depth[rv]) {
swap(ru, rv);
}
par[ru] = rv;
if (depth[ru] == depth[rv]) {
depth[rv]++;
}
return true;
}
long long minimum_walk(vector<int> a, int s) {
int n = a.size();
for (int i = 0; i < n; i++) {
par[i] = -1;
}
long long res = 0;
for (int i = 0; i < n; i++) {
res += abs(a[i] - i);
if (i != s && a[i] == i) {
skip[i] = true;
}
pref[min(a[i], i)]++;
pref[max(a[i], i)]--;
}
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[i] += pref[i - 1];
}
if (pref[i] > 0) {
update(i, i + 1);
}
}
nxt[n - 1] = -1;
for (int i = n - 2; i >= 0; i--) {
if (!skip[i + 1]) {
nxt[i] = i + 1;
}
else {
nxt[i] = nxt[i + 1];
}
}
vector<pair<int, pair<int, int>>> edges;
for (int i = 0; i < n - 1; i++) {
if (!skip[i] && nxt[i] != -1) {
edges.emplace_back(nxt[i] - i, make_pair(i, nxt[i]));
}
}
sort(edges.begin(), edges.end());
for (auto e: edges) {
if (update(e.second.first, e.second.second)) {
res += e.first * 2;
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 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 |
340 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 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 |
340 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 |
340 KB |
Output is correct |
30 |
Correct |
126 ms |
32228 KB |
Output is correct |
31 |
Correct |
171 ms |
35748 KB |
Output is correct |
32 |
Correct |
90 ms |
24224 KB |
Output is correct |
33 |
Correct |
149 ms |
34436 KB |
Output is correct |
34 |
Correct |
164 ms |
34572 KB |
Output is correct |
35 |
Correct |
165 ms |
40676 KB |
Output is correct |
36 |
Correct |
178 ms |
40716 KB |
Output is correct |
37 |
Correct |
203 ms |
40624 KB |
Output is correct |
38 |
Correct |
243 ms |
39128 KB |
Output is correct |
39 |
Correct |
213 ms |
38788 KB |
Output is correct |
40 |
Correct |
206 ms |
36288 KB |
Output is correct |
41 |
Correct |
141 ms |
35768 KB |
Output is correct |
42 |
Correct |
172 ms |
36128 KB |
Output is correct |
43 |
Correct |
182 ms |
30528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 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 |
340 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 |
340 KB |
Output is correct |
30 |
Correct |
126 ms |
32228 KB |
Output is correct |
31 |
Correct |
171 ms |
35748 KB |
Output is correct |
32 |
Correct |
90 ms |
24224 KB |
Output is correct |
33 |
Correct |
149 ms |
34436 KB |
Output is correct |
34 |
Correct |
164 ms |
34572 KB |
Output is correct |
35 |
Correct |
165 ms |
40676 KB |
Output is correct |
36 |
Correct |
178 ms |
40716 KB |
Output is correct |
37 |
Correct |
203 ms |
40624 KB |
Output is correct |
38 |
Correct |
243 ms |
39128 KB |
Output is correct |
39 |
Correct |
213 ms |
38788 KB |
Output is correct |
40 |
Correct |
206 ms |
36288 KB |
Output is correct |
41 |
Correct |
141 ms |
35768 KB |
Output is correct |
42 |
Correct |
172 ms |
36128 KB |
Output is correct |
43 |
Correct |
182 ms |
30528 KB |
Output is correct |
44 |
Incorrect |
1 ms |
340 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
45 |
Halted |
0 ms |
0 KB |
- |