# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
526878 |
2022-02-16T13:40:30 Z |
KoD |
Ancient Books (IOI17_books) |
C++17 |
|
810 ms |
208124 KB |
#include <bits/stdc++.h>
#include "books.h"
using std::vector;
using std::array;
using std::pair;
using std::tuple;
using ll = long long;
struct UnionFind {
vector<int> par;
UnionFind(const int n) : par(n, -1) {}
int find(const int u) {
return par[u] < 0 ? u : par[u] = find(par[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (par[u] > par[v]) std::swap(u, v);
par[u] += par[v];
par[v] = u;
return;
}
};
ll minimum_walk(vector<int> P, int src) {
{
int l = 0;
while (l < src and P[l] == l) l += 1;
int r = (int)P.size() - 1;
while (r > src and P[r] == r) r -= 1;
P.erase(P.begin() + r + 1, P.end());
P.erase(P.begin(), P.begin() + l);
for (auto& x : P) x -= l;
src -= l;
}
const int N = P.size();
vector<vector<int>> cycle;
{
vector<char> done(N);
for (int i = 0; i < N; ++i) {
if (done[i]) continue;
auto& v = cycle.emplace_back();
for (int j = i; !done[j]; j = P[j]) {
v.push_back(j);
done[j] = true;
}
}
}
const int M = cycle.size();
UnionFind dsu(M);
{
vector<tuple<int, int, int>> query;
for (int i = 0; i < M; ++i) {
auto& v = cycle[i];
std::sort(v.begin(), v.end());
for (int j = 1; j < (int)v.size(); ++j) {
query.emplace_back(N + v[j - 1], N + v[j], i);
}
}
std::sort(query.begin(), query.end());
vector<vector<int>> list(2 * N);
const auto process = [&](const int k, const int i) {
for (const int j : list[k]) dsu.merge(i, j);
// list[k] = {i};
};
for (const auto& [lc, rc, i] : query) {
for (int l = lc, r = rc; l < r; l >>= 1, r >>= 1) {
if (l & 1) process(l++, i);
if (r & 1) process(--r, i);
}
for (int k = rc; k > 0; k >>= 1) list[k].push_back(i);
}
}
vector<int> belongs(N);
for (int i = 0; i < M; ++i) {
const int p = dsu.find(i);
for (const int j : cycle[i]) {
belongs[j] = p;
}
}
vector<vector<int>> list(M);
for (int i = 0; i < N; ++i) list[belongs[i]].push_back(i);
vector<int> parent(M, -1);
{
vector<int> start(N, -1), end(N, -1);
for (int j = 0; j < M; ++j) {
if (!list[j].empty()) {
start[list[j].front()] = j;
end[list[j].back()] = j;
}
}
vector<int> stack;
for (int i = 0; i < N; ++i) {
if (start[i] != -1) {
if (!stack.empty()) parent[start[i]] = stack.back();
stack.push_back(start[i]);
}
if (end[i] != -1) stack.pop_back();
}
}
vector<int> dist(M, N + 1);
{
std::queue<int> que;
const auto push = [&](const int u, const int d) {
if (dist[u] > d) {
dist[u] = d;
que.push(u);
}
};
push(belongs[src], 0);
while (!que.empty()) {
const int u = que.front();
que.pop();
const int l = list[u].front();
const int r = list[u].back();
if (l > 0) push(belongs[l - 1], dist[u] + 1);
if (r + 1 < N) push(belongs[r + 1], dist[u] + 1);
}
}
int root = belongs[src];
while (parent[root] != -1) root = parent[root];
ll ret = 2 * dist[root];
vector<int> coeff(N);
for (int i = 0; i < N; ++i) {
const int l = std::min(i, P[i]);
const int r = std::max(i, P[i]);
ret += r - l;
coeff[l] += 1;
coeff[r] -= 1;
}
for (int i = 0; i < N - 1; ++i) {
if (coeff[i] == 0) ret += 2;
coeff[i + 1] += coeff[i];
}
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
761 ms |
199828 KB |
Output is correct |
31 |
Correct |
810 ms |
199900 KB |
Output is correct |
32 |
Correct |
79 ms |
8044 KB |
Output is correct |
33 |
Correct |
491 ms |
156272 KB |
Output is correct |
34 |
Correct |
463 ms |
156428 KB |
Output is correct |
35 |
Correct |
520 ms |
165268 KB |
Output is correct |
36 |
Correct |
573 ms |
193308 KB |
Output is correct |
37 |
Correct |
632 ms |
200924 KB |
Output is correct |
38 |
Correct |
640 ms |
201372 KB |
Output is correct |
39 |
Correct |
637 ms |
201244 KB |
Output is correct |
40 |
Correct |
688 ms |
200668 KB |
Output is correct |
41 |
Correct |
702 ms |
200024 KB |
Output is correct |
42 |
Correct |
706 ms |
200348 KB |
Output is correct |
43 |
Correct |
493 ms |
149224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
404 KB |
Output is correct |
8 |
Correct |
1 ms |
420 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
420 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
420 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
420 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
208 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
761 ms |
199828 KB |
Output is correct |
31 |
Correct |
810 ms |
199900 KB |
Output is correct |
32 |
Correct |
79 ms |
8044 KB |
Output is correct |
33 |
Correct |
491 ms |
156272 KB |
Output is correct |
34 |
Correct |
463 ms |
156428 KB |
Output is correct |
35 |
Correct |
520 ms |
165268 KB |
Output is correct |
36 |
Correct |
573 ms |
193308 KB |
Output is correct |
37 |
Correct |
632 ms |
200924 KB |
Output is correct |
38 |
Correct |
640 ms |
201372 KB |
Output is correct |
39 |
Correct |
637 ms |
201244 KB |
Output is correct |
40 |
Correct |
688 ms |
200668 KB |
Output is correct |
41 |
Correct |
702 ms |
200024 KB |
Output is correct |
42 |
Correct |
706 ms |
200348 KB |
Output is correct |
43 |
Correct |
493 ms |
149224 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
404 KB |
Output is correct |
51 |
Correct |
1 ms |
420 KB |
Output is correct |
52 |
Correct |
1 ms |
332 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
1 ms |
420 KB |
Output is correct |
55 |
Correct |
2 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
460 KB |
Output is correct |
58 |
Correct |
1 ms |
460 KB |
Output is correct |
59 |
Correct |
2 ms |
420 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
420 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
462 ms |
154888 KB |
Output is correct |
65 |
Correct |
440 ms |
146840 KB |
Output is correct |
66 |
Correct |
659 ms |
207532 KB |
Output is correct |
67 |
Correct |
649 ms |
207876 KB |
Output is correct |
68 |
Correct |
51 ms |
18868 KB |
Output is correct |
69 |
Correct |
64 ms |
19468 KB |
Output is correct |
70 |
Correct |
62 ms |
18404 KB |
Output is correct |
71 |
Correct |
56 ms |
16072 KB |
Output is correct |
72 |
Correct |
35 ms |
14100 KB |
Output is correct |
73 |
Correct |
60 ms |
19668 KB |
Output is correct |
74 |
Correct |
484 ms |
163080 KB |
Output is correct |
75 |
Correct |
489 ms |
163028 KB |
Output is correct |
76 |
Correct |
494 ms |
158280 KB |
Output is correct |
77 |
Correct |
490 ms |
157676 KB |
Output is correct |
78 |
Correct |
501 ms |
166816 KB |
Output is correct |
79 |
Correct |
522 ms |
166748 KB |
Output is correct |
80 |
Correct |
83 ms |
14792 KB |
Output is correct |
81 |
Correct |
459 ms |
166428 KB |
Output is correct |
82 |
Correct |
479 ms |
166436 KB |
Output is correct |
83 |
Correct |
628 ms |
195048 KB |
Output is correct |
84 |
Correct |
649 ms |
198860 KB |
Output is correct |
85 |
Correct |
617 ms |
204428 KB |
Output is correct |
86 |
Correct |
679 ms |
207156 KB |
Output is correct |
87 |
Correct |
408 ms |
143360 KB |
Output is correct |
88 |
Correct |
519 ms |
159820 KB |
Output is correct |
89 |
Correct |
650 ms |
187304 KB |
Output is correct |
90 |
Correct |
637 ms |
208124 KB |
Output is correct |
91 |
Correct |
672 ms |
208056 KB |
Output is correct |
92 |
Correct |
665 ms |
207596 KB |
Output is correct |