#include <bits/stdc++.h>
#define ll long long
#include "books.h"
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ar array
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
struct seg {
int N; vector<vector<ar<int, 2>>> adj;
seg(int n) : N(1<<(__lg(n)+1)), adj(2*N) {
for (int i = 2; i < 2*N; i++) adj[i].push_back({i >> 1, 0});
}
void add_edge(int x, int y) {
adj[x+N].push_back({y+N, 1});
}
void add_edge(int node, int nl, int nr, int ql, int qr, int x) { // l and r go to x
if (ql > nr || qr < nl) return;
if (ql <= nl && nr <= qr) {
adj[node].push_back({x + N, 0});
return;
}
int mid = (nl+nr)/2;
add_edge(node * 2, nl, mid, ql, qr, x);
add_edge(node * 2 + 1, mid + 1, nr, ql, qr, x);
}
void bfs(vector<int>& dist, vector<int> nodes) {
vector<int> dist2(2 * N, 1e9);
for (int i = 0; i < sz(dist); i++) dist2[i + N] = dist[i];
priority_queue<ar<int, 2>, vector<ar<int, 2>>, greater<ar<int, 2>>> q;
for (int v : nodes) q.push({dist[v], v + N});
while (q.size()) {
auto [cost, v] = q.top(); q.pop();
if (cost != dist2[v]) continue;
for (auto [u, w] : adj[v]) if (ckmin(dist2[u], cost + w)) q.push({dist2[u], u});
}
for (int i = 0; i < sz(dist); i++) dist[i] = dist2[i + N];
}
};
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
{
int l = s, r = s;
for (int i = s; i >= 0; i--) if (i != p[i]) l = i;
for (int i = s; i < n; i++) if (i != p[i]) r = i;
p = vector<int>(p.begin() + l, p.begin() + r + 1);
n = r - l + 1;
}
seg st(n);
for (int i = 0; i < n; i++) {
if (i) st.add_edge(i, i-1);
if (i+1 < n) st.add_edge(i, i+1);
}
vector<bool> vis(n);
ll ans = 0;
for (int i = 0; i < n; i++) if (!vis[i]) {
int L = i, R = i, tmp = i;
vector<int> pth;
while (!vis[tmp]) {
ans += abs(tmp - p[tmp]);
ckmax(R, tmp), vis[tmp] = 1;
pth.emplace_back(tmp);
tmp = p[tmp];
}
for (int v : pth) st.add_edge(1, 0, st.N-1, L, R, v);
}
vector<int> d1(n, 1e9), d2(n, 1e9);
d1[0] = 0, d2[n-1] = 0;
st.bfs(d1, {0});
st.bfs(d2, {n-1});
vector<int> d3(n);
for (int i = 0; i < n; i++) d3[i] = d1[i] + d2[i];
vector<int> tot(n);
iota(all(tot), 0);
st.bfs(d3, tot);
return ans + 2 * d3[s];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
632 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
632 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1262 ms |
306852 KB |
Output is correct |
31 |
Correct |
1495 ms |
378916 KB |
Output is correct |
32 |
Correct |
51 ms |
8016 KB |
Output is correct |
33 |
Correct |
663 ms |
199428 KB |
Output is correct |
34 |
Correct |
689 ms |
199432 KB |
Output is correct |
35 |
Correct |
750 ms |
207624 KB |
Output is correct |
36 |
Correct |
898 ms |
229540 KB |
Output is correct |
37 |
Correct |
910 ms |
271780 KB |
Output is correct |
38 |
Correct |
968 ms |
294332 KB |
Output is correct |
39 |
Correct |
907 ms |
294912 KB |
Output is correct |
40 |
Correct |
951 ms |
315144 KB |
Output is correct |
41 |
Correct |
1176 ms |
331724 KB |
Output is correct |
42 |
Correct |
1147 ms |
370964 KB |
Output is correct |
43 |
Execution timed out |
2043 ms |
226776 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Incorrect |
0 ms |
348 KB |
3rd lines differ - on the 1st token, expected: '0', found: '1998' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
632 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1262 ms |
306852 KB |
Output is correct |
31 |
Correct |
1495 ms |
378916 KB |
Output is correct |
32 |
Correct |
51 ms |
8016 KB |
Output is correct |
33 |
Correct |
663 ms |
199428 KB |
Output is correct |
34 |
Correct |
689 ms |
199432 KB |
Output is correct |
35 |
Correct |
750 ms |
207624 KB |
Output is correct |
36 |
Correct |
898 ms |
229540 KB |
Output is correct |
37 |
Correct |
910 ms |
271780 KB |
Output is correct |
38 |
Correct |
968 ms |
294332 KB |
Output is correct |
39 |
Correct |
907 ms |
294912 KB |
Output is correct |
40 |
Correct |
951 ms |
315144 KB |
Output is correct |
41 |
Correct |
1176 ms |
331724 KB |
Output is correct |
42 |
Correct |
1147 ms |
370964 KB |
Output is correct |
43 |
Execution timed out |
2043 ms |
226776 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |