//Cgrader.cpp
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
using ll = long long;
using ld = long double;
const int N = 1e6 + 10;
int cc[N], cnt[N];
vector<tuple<int, int, int>> edges;
struct DSU {
int rank[N], parent[N];
DSU() {
iota(all(parent), 0);
}
int get(int u) {
if (u == parent[u]) {
return u;
}
return parent[u] = get(parent[u]);
}
bool merge(int a, int b) {
a = get(a);
b = get(b);
if (a == b) {
return false;
}
if (rank[a] == rank[b]) {
++rank[a];
}
if (rank[a] < rank[b]) {
swap(a, b);
}
parent[b] = a;
return true;
}
} dsu;
ll minimum_walk(vector<int> p, int s) {
int n = sz(p), m = 0;
ll ans = 0;
fill(all(cc), -1);
for (int i = 0; i < n; ++i) {
ans += abs(p[i] - i);
if (cc[i] == -1) {
int j = i;
while (cc[j] == -1) {
cc[j] = m;
cnt[m]++;
j = p[j];
}
++m;
}
}
// cout << ans << endl;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (cc[i] != cc[j]) {
edges.push_back({abs(i - j), cc[i], cc[j]});
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = min(p[i], i); j < max(p[i], i); ++j) {
if (cc[i] != cc[j]) {
edges.push_back({0, cc[i], cc[j]});
}
}
}
sort(all(edges));
for (auto [w, u, v] : edges) {
if (cnt[u] == 1 && u != cc[s]) {
continue;
}
if (cnt[v] == 1 && v != cc[s]) {
continue;
}
if (dsu.merge(u, v)) {
ans += 2 * w;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8080 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
6 ms |
8020 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
6 ms |
8064 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8080 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
6 ms |
8020 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
6 ms |
8064 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
58 ms |
20564 KB |
Output is correct |
20 |
Correct |
48 ms |
14404 KB |
Output is correct |
21 |
Correct |
105 ms |
14408 KB |
Output is correct |
22 |
Correct |
60 ms |
14404 KB |
Output is correct |
23 |
Correct |
59 ms |
14404 KB |
Output is correct |
24 |
Correct |
67 ms |
14460 KB |
Output is correct |
25 |
Correct |
69 ms |
14468 KB |
Output is correct |
26 |
Correct |
67 ms |
14428 KB |
Output is correct |
27 |
Correct |
66 ms |
14464 KB |
Output is correct |
28 |
Correct |
60 ms |
14456 KB |
Output is correct |
29 |
Correct |
71 ms |
20528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8080 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
6 ms |
8020 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
6 ms |
8064 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
58 ms |
20564 KB |
Output is correct |
20 |
Correct |
48 ms |
14404 KB |
Output is correct |
21 |
Correct |
105 ms |
14408 KB |
Output is correct |
22 |
Correct |
60 ms |
14404 KB |
Output is correct |
23 |
Correct |
59 ms |
14404 KB |
Output is correct |
24 |
Correct |
67 ms |
14460 KB |
Output is correct |
25 |
Correct |
69 ms |
14468 KB |
Output is correct |
26 |
Correct |
67 ms |
14428 KB |
Output is correct |
27 |
Correct |
66 ms |
14464 KB |
Output is correct |
28 |
Correct |
60 ms |
14456 KB |
Output is correct |
29 |
Correct |
71 ms |
20528 KB |
Output is correct |
30 |
Runtime error |
1622 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
59 ms |
14404 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 |
3 ms |
8148 KB |
Output is correct |
2 |
Correct |
4 ms |
8148 KB |
Output is correct |
3 |
Correct |
3 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8080 KB |
Output is correct |
5 |
Correct |
4 ms |
8148 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
4 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8152 KB |
Output is correct |
10 |
Correct |
6 ms |
8020 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
6 ms |
8064 KB |
Output is correct |
16 |
Correct |
4 ms |
8148 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
58 ms |
20564 KB |
Output is correct |
20 |
Correct |
48 ms |
14404 KB |
Output is correct |
21 |
Correct |
105 ms |
14408 KB |
Output is correct |
22 |
Correct |
60 ms |
14404 KB |
Output is correct |
23 |
Correct |
59 ms |
14404 KB |
Output is correct |
24 |
Correct |
67 ms |
14460 KB |
Output is correct |
25 |
Correct |
69 ms |
14468 KB |
Output is correct |
26 |
Correct |
67 ms |
14428 KB |
Output is correct |
27 |
Correct |
66 ms |
14464 KB |
Output is correct |
28 |
Correct |
60 ms |
14456 KB |
Output is correct |
29 |
Correct |
71 ms |
20528 KB |
Output is correct |
30 |
Runtime error |
1622 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Halted |
0 ms |
0 KB |
- |