#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
#define pii pair <int, int>
#define pb push_back
const int N = 1e6 + 5,inf = 1e9;
int n, p[N],fix[N],l[N],r[N],cur,pr[N],idx[N],cnt,opp[N];
int tree[3][4 * N];
void build(int node, int le, int ri) {
if (le == ri) {
if (opp[le] && opp[le] >= le) tree[0][node] = opp[le];
if (opp[le] && opp[le] <= le) tree[1][node] = opp[le];
return ;
}
int mid = (le + ri) / 2;
build(2 * node, le, mid);
build(2 * node + 1, mid + 1, ri);
tree[0][node] = max(tree[0][2 * node], tree[0][2 * node + 1]);
tree[1][node] = min(tree[1][2 * node], tree[1][2 * node + 1]);
}
void update(int node, int le, int ri, int idx, int val, int ty) {
if (le > idx || ri < idx) return ;
if (le == ri) {
tree[ty][node] = val;
return ;
}
int mid = (le + ri) / 2;
update(2 * node, le, mid, idx, val, ty);
update(2 * node + 1, mid + 1, ri, idx, val, ty);
tree[0][node] = max(tree[0][2 * node], tree[0][2 * node + 1]);
tree[1][node] = min(tree[1][2 * node], tree[1][2 * node + 1]);
}
int getans(int node, int le, int ri, int start, int end, int ty) {
if (le > end || ri < start) {
return (ty == 0 ? -inf : inf);
}
if (le >= start && ri <= end) return tree[ty][node];
int mid = (le + ri) / 2;
int p1 = getans(2 * node, le, mid, start, end, ty);
int p2 = getans(2 * node + 1, mid + 1, ri, start, end, ty);
return (ty == 0 ? max(p1, p2) : min(p1, p2));
}
vector <pii> g[N];
void add_edge(int a, int b, int c) {
g[a].pb({b, c});
g[b].pb({a, c});
}
int dist[N];
int bfs01(int s, int l, int r) {
deque < int > dq;
for (int i = 1; i <= n; i++) {
dist[i] = inf;
}
dist[s] = 0;
dq.pb(s);
while (!dq.empty()) {
int vert = dq[0];
if (vert == l || vert == r) return dist[vert];
dq.pop_front();
for (pii sth : g[vert]) {
int to = sth.f, c = sth.s;
if (dist[to] > dist[vert] + c) {
dist[to] = dist[vert] + c;
if (c == 0) dq.push_front(to);
else dq.push_back(to);
}
}
}
assert(false);
}
long long minimum_walk(vector<int> perm, int s) {
s++;
n = perm.size();
long long ans = 0;
for (int i = 1; i <= n; i++) {
p[i] = perm[i - 1] + 1;
ans += (ll)abs(i - p[i]);
}
int frst_id = -1, lst_id = -1;
for (int i = 1; i <= n ;i++) {
if (p[i] != i && frst_id == -1) frst_id = i;
if (p[i] != i) lst_id = i;
}
if (frst_id == -1) {
return 0;
}
vector <pii> segs;
for (int i = 1; i <= n; i++) {
int cur = p[i];
if (!fix[i]) {
fix[i] = 1;
idx[i] = ++cnt;
l[cnt] = r[cnt] = i;
vector <int> vec;
vec.pb(i);
while (cur != i) {
vec.pb(cur);
fix[cur] = 1;
l[cnt] = min(l[cnt], cur);
r[cnt] = max(r[cnt], cur);
cur = p[cur];
}
sort(vec.begin(), vec.end());
for (int i = 1; i < (int)vec.size(); i++) {
add_edge(vec[i], vec[i - 1], 0);
segs.pb({vec[i - 1], vec[i]});
}
opp[l[cnt]] = r[cnt];
opp[r[cnt]] = l[cnt];
pr[l[cnt]]++;
pr[r[cnt]]--;
}
}
for (int i = 1; i <= n; i++) {
pr[i] += pr[i - 1];
}
for (int i = 1; i <= n; i++) {
if (pr[i] == 0 && i < lst_id && i >= frst_id) ans += 2;
else {
if (pr[i] == 0 && s <= i && i < lst_id) ans += 2;
else if (pr[i] == 0 && s > i && i >= frst_id) ans += 2;
}
}
for (int i = 1; i < n; i++) {
add_edge(i, i + 1, 1);
}
int posi = s, posj = s;
sort(segs.begin(), segs.end());
build(1, 1, n);
for (pii sth : segs) {
int l = sth.f, r = sth.s;
// Left E [l + 1, r - 1] maximum Right
int mx = getans(1, 1, n, l + 1, r - 1, 0);
if (mx > r) {
add_edge(l, mx, 0);
}
// Right E [l + 1, r - 1], minimum left
int mn = getans(1, 1, n, l + 1, r - 1, 1);
if (mn < l) {
add_edge(r, mn, 0);
}
}
int lid = 1, rid = n;
for (int i = 0; i < s; i++) {
if (!pr[i]) lid = i + 1;
}
for (int i = s; i <= n; i++) {
if (!pr[i]) { rid = i; break; }
}
int to_add = bfs01(s, lid, rid);
ans += (ll)2 * (ll)to_add;
return ans;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:131:9: warning: unused variable 'posi' [-Wunused-variable]
131 | int posi = s, posj = s;
| ^~~~
books.cpp:131:19: warning: unused variable 'posj' [-Wunused-variable]
131 | int posi = s, posj = s;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23840 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23692 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
12 ms |
23848 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23752 KB |
Output is correct |
16 |
Correct |
14 ms |
23892 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23840 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23692 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
12 ms |
23848 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23752 KB |
Output is correct |
16 |
Correct |
14 ms |
23892 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23892 KB |
Output is correct |
20 |
Correct |
12 ms |
23932 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23932 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23892 KB |
Output is correct |
27 |
Correct |
12 ms |
23892 KB |
Output is correct |
28 |
Correct |
13 ms |
23916 KB |
Output is correct |
29 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23840 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23692 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
12 ms |
23848 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23752 KB |
Output is correct |
16 |
Correct |
14 ms |
23892 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23892 KB |
Output is correct |
20 |
Correct |
12 ms |
23932 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23932 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23892 KB |
Output is correct |
27 |
Correct |
12 ms |
23892 KB |
Output is correct |
28 |
Correct |
13 ms |
23916 KB |
Output is correct |
29 |
Correct |
12 ms |
23892 KB |
Output is correct |
30 |
Correct |
711 ms |
136100 KB |
Output is correct |
31 |
Correct |
761 ms |
152156 KB |
Output is correct |
32 |
Correct |
91 ms |
42188 KB |
Output is correct |
33 |
Correct |
345 ms |
127028 KB |
Output is correct |
34 |
Correct |
340 ms |
126884 KB |
Output is correct |
35 |
Correct |
436 ms |
135220 KB |
Output is correct |
36 |
Correct |
569 ms |
149268 KB |
Output is correct |
37 |
Correct |
669 ms |
157640 KB |
Output is correct |
38 |
Correct |
689 ms |
156116 KB |
Output is correct |
39 |
Correct |
706 ms |
155676 KB |
Output is correct |
40 |
Correct |
695 ms |
145544 KB |
Output is correct |
41 |
Correct |
703 ms |
137368 KB |
Output is correct |
42 |
Correct |
706 ms |
149624 KB |
Output is correct |
43 |
Correct |
375 ms |
129448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23932 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
11 ms |
23860 KB |
Output is correct |
5 |
Correct |
12 ms |
23908 KB |
Output is correct |
6 |
Correct |
12 ms |
23892 KB |
Output is correct |
7 |
Correct |
12 ms |
23892 KB |
Output is correct |
8 |
Correct |
12 ms |
23920 KB |
Output is correct |
9 |
Correct |
14 ms |
23948 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23892 KB |
Output is correct |
12 |
Correct |
14 ms |
23916 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
12 ms |
23928 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Correct |
12 ms |
23980 KB |
Output is correct |
17 |
Correct |
12 ms |
23888 KB |
Output is correct |
18 |
Correct |
12 ms |
23868 KB |
Output is correct |
19 |
Correct |
13 ms |
23892 KB |
Output is correct |
20 |
Correct |
11 ms |
24020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23832 KB |
Output is correct |
3 |
Correct |
11 ms |
23868 KB |
Output is correct |
4 |
Correct |
11 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23840 KB |
Output is correct |
7 |
Correct |
11 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23692 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23748 KB |
Output is correct |
12 |
Correct |
12 ms |
23848 KB |
Output is correct |
13 |
Correct |
12 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23752 KB |
Output is correct |
16 |
Correct |
14 ms |
23892 KB |
Output is correct |
17 |
Correct |
12 ms |
23744 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23892 KB |
Output is correct |
20 |
Correct |
12 ms |
23932 KB |
Output is correct |
21 |
Correct |
11 ms |
23764 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
12 ms |
23932 KB |
Output is correct |
24 |
Correct |
12 ms |
23892 KB |
Output is correct |
25 |
Correct |
11 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23892 KB |
Output is correct |
27 |
Correct |
12 ms |
23892 KB |
Output is correct |
28 |
Correct |
13 ms |
23916 KB |
Output is correct |
29 |
Correct |
12 ms |
23892 KB |
Output is correct |
30 |
Correct |
711 ms |
136100 KB |
Output is correct |
31 |
Correct |
761 ms |
152156 KB |
Output is correct |
32 |
Correct |
91 ms |
42188 KB |
Output is correct |
33 |
Correct |
345 ms |
127028 KB |
Output is correct |
34 |
Correct |
340 ms |
126884 KB |
Output is correct |
35 |
Correct |
436 ms |
135220 KB |
Output is correct |
36 |
Correct |
569 ms |
149268 KB |
Output is correct |
37 |
Correct |
669 ms |
157640 KB |
Output is correct |
38 |
Correct |
689 ms |
156116 KB |
Output is correct |
39 |
Correct |
706 ms |
155676 KB |
Output is correct |
40 |
Correct |
695 ms |
145544 KB |
Output is correct |
41 |
Correct |
703 ms |
137368 KB |
Output is correct |
42 |
Correct |
706 ms |
149624 KB |
Output is correct |
43 |
Correct |
375 ms |
129448 KB |
Output is correct |
44 |
Correct |
12 ms |
23892 KB |
Output is correct |
45 |
Correct |
12 ms |
23932 KB |
Output is correct |
46 |
Correct |
12 ms |
23892 KB |
Output is correct |
47 |
Correct |
11 ms |
23860 KB |
Output is correct |
48 |
Correct |
12 ms |
23908 KB |
Output is correct |
49 |
Correct |
12 ms |
23892 KB |
Output is correct |
50 |
Correct |
12 ms |
23892 KB |
Output is correct |
51 |
Correct |
12 ms |
23920 KB |
Output is correct |
52 |
Correct |
14 ms |
23948 KB |
Output is correct |
53 |
Correct |
11 ms |
23764 KB |
Output is correct |
54 |
Correct |
12 ms |
23892 KB |
Output is correct |
55 |
Correct |
14 ms |
23916 KB |
Output is correct |
56 |
Correct |
12 ms |
23892 KB |
Output is correct |
57 |
Correct |
12 ms |
23928 KB |
Output is correct |
58 |
Correct |
12 ms |
23892 KB |
Output is correct |
59 |
Correct |
12 ms |
23980 KB |
Output is correct |
60 |
Correct |
12 ms |
23888 KB |
Output is correct |
61 |
Correct |
12 ms |
23868 KB |
Output is correct |
62 |
Correct |
13 ms |
23892 KB |
Output is correct |
63 |
Correct |
11 ms |
24020 KB |
Output is correct |
64 |
Correct |
347 ms |
126720 KB |
Output is correct |
65 |
Correct |
314 ms |
123428 KB |
Output is correct |
66 |
Correct |
686 ms |
157420 KB |
Output is correct |
67 |
Correct |
741 ms |
158000 KB |
Output is correct |
68 |
Correct |
63 ms |
36620 KB |
Output is correct |
69 |
Correct |
70 ms |
37320 KB |
Output is correct |
70 |
Correct |
61 ms |
36340 KB |
Output is correct |
71 |
Correct |
46 ms |
34792 KB |
Output is correct |
72 |
Correct |
45 ms |
33868 KB |
Output is correct |
73 |
Correct |
73 ms |
36752 KB |
Output is correct |
74 |
Correct |
343 ms |
126872 KB |
Output is correct |
75 |
Correct |
347 ms |
126920 KB |
Output is correct |
76 |
Correct |
380 ms |
129948 KB |
Output is correct |
77 |
Correct |
394 ms |
129516 KB |
Output is correct |
78 |
Correct |
428 ms |
135992 KB |
Output is correct |
79 |
Correct |
481 ms |
136016 KB |
Output is correct |
80 |
Correct |
88 ms |
42188 KB |
Output is correct |
81 |
Correct |
485 ms |
136780 KB |
Output is correct |
82 |
Correct |
473 ms |
136864 KB |
Output is correct |
83 |
Correct |
617 ms |
141996 KB |
Output is correct |
84 |
Correct |
605 ms |
147192 KB |
Output is correct |
85 |
Correct |
675 ms |
155060 KB |
Output is correct |
86 |
Correct |
715 ms |
157304 KB |
Output is correct |
87 |
Correct |
334 ms |
123528 KB |
Output is correct |
88 |
Correct |
386 ms |
131072 KB |
Output is correct |
89 |
Correct |
530 ms |
137256 KB |
Output is correct |
90 |
Correct |
780 ms |
157912 KB |
Output is correct |
91 |
Correct |
723 ms |
158056 KB |
Output is correct |
92 |
Correct |
739 ms |
148388 KB |
Output is correct |