#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define SZ(i) int(i.size())
#define ALL(i) i.begin(), i.end()
#define MEM(i,a) memset(i, (a), sizeof(i))
#define X first
#define Y second
#define eb emplace_back
#define pb push_back
#ifdef tmd
#define debug(...) fprintf(stderr,"#%d-(%s)=",__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do (T &&x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y) {cerr<<x<<", ";_do(y...);}
template<typename IT> ostream& __printRng (ostream &os, IT bg, IT ed) {
for (IT it=bg; it!=ed; it++) {
if (it!=bg) os << "," << *it;
else os << "{" << *it;
}
return os << "}";
}
template<typename T> ostream &operator << (ostream &os, const pair<T,T> &v) {
return os << "{" << v.X << "," <<v.Y << "}";
}
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) {
return __printRng(os, ALL(v));
}
template<typename T> ostream &operator << (ostream &os, const deque<T> &v) {
return __printRng(os, ALL(v));
}
template<typename T> ostream &operator << (ostream &os, const set<T> &v) {
return __printRng(os, ALL(v));
}
#define IOS()
#else
#define debug(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif
#include "books.h"
int n, mn, mx;
ll sum;
vector<int> vis, p;
void dfs (int nd) {
mn = min(nd, mn);
mx = max(nd, mx);
vis[nd] = true;
sum += abs(nd-p[nd]);
if (!vis[p[nd]]) {
dfs(p[nd]);
}
}
ll minimum_walk(std::vector<int> P, int s) {
p = P;
n = SZ(p);
vis.clear();
vis.resize(n, 0);
sum = 0;
vector<pii> rng;
for (int i=0; i<n; i++) {
if (!vis[i] && p[i] != i) {
mn = i;
mx = i;
dfs(i);
rng.eb(mn, mx);
}
}
sort(ALL(rng));
vector<pii> nrng;
int lst = -1;
int L = -1;
for (pii r : rng) {
if (r.X > lst) {
if (L != -1) {
nrng.eb(L, lst);
}
L = r.X;
lst = r.Y;
} else {
lst = max(lst, r.Y);
}
}
if (L==-1) return 0;
nrng.eb(L, lst);
debug(nrng);
debug(sum);
for (int i=1; i<SZ(nrng); i++) {
sum += 2 * (nrng[i].X - nrng[i-1].Y);
}
if (s <= nrng[0].X) {
sum += 2 * (nrng[0].X - s);
} else if (s >= nrng.back().Y) {
sum += 2 * (s - nrng.back().Y);
} else {
pii bg;
for (auto v : nrng) {
if (v.Y >= s) {
bg = v;
break;
}
}
if (bg.X < s) {
int sz = bg.Y - bg.X + 1;
set<int> alive;
deque<int> bfs;
set<int> vs;
vector<int> dis(n, 0x3f3f3f3f);
for (int i=bg.X; i<=bg.Y; i++) {
alive.insert(i);
}
alive.erase(s);
bfs.emplace_back(s);
dis[s] = 0;
while (bfs.size()) {
debug(bfs);
int nd = bfs.front();
bfs.pop_front();
if (vs.count(nd)) continue;
vs.insert(nd);
int l = nd, r = p[nd];
if (l > r) swap(l, r);
while (l < r) {
auto ptr = alive.lower_bound(l);
if (ptr == alive.end() || *ptr > r) {
break;
}
bfs.emplace_front(*ptr);
dis[*ptr] = min(dis[*ptr], dis[nd]);
l = *ptr;
alive.erase(ptr);
}
if (alive.count(nd+1)) {
dis[nd+1] = min(dis[nd+1], dis[nd] + 1);
bfs.emplace_back(nd + 1);
}
if (alive.count(nd-1)) {
dis[nd-1] = min(dis[nd-1], dis[nd] + 1);
bfs.emplace_back(nd - 1);
}
}
sum += min(dis[bg.X], dis[bg.Y]) * 2;
debug(dis);
}
}
return sum;
}
Compilation message
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:117:17: warning: unused variable 'sz' [-Wunused-variable]
117 | int sz = bg.Y - bg.X + 1;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
238 ms |
15992 KB |
Output is correct |
31 |
Correct |
246 ms |
15992 KB |
Output is correct |
32 |
Correct |
140 ms |
15996 KB |
Output is correct |
33 |
Correct |
154 ms |
20584 KB |
Output is correct |
34 |
Correct |
153 ms |
20584 KB |
Output is correct |
35 |
Correct |
156 ms |
20712 KB |
Output is correct |
36 |
Correct |
156 ms |
18864 KB |
Output is correct |
37 |
Correct |
151 ms |
16376 KB |
Output is correct |
38 |
Correct |
151 ms |
16064 KB |
Output is correct |
39 |
Correct |
160 ms |
16124 KB |
Output is correct |
40 |
Correct |
179 ms |
15992 KB |
Output is correct |
41 |
Correct |
198 ms |
15992 KB |
Output is correct |
42 |
Correct |
183 ms |
15992 KB |
Output is correct |
43 |
Correct |
154 ms |
18156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
0 ms |
256 KB |
Output is correct |
17 |
Correct |
0 ms |
256 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
238 ms |
15992 KB |
Output is correct |
31 |
Correct |
246 ms |
15992 KB |
Output is correct |
32 |
Correct |
140 ms |
15996 KB |
Output is correct |
33 |
Correct |
154 ms |
20584 KB |
Output is correct |
34 |
Correct |
153 ms |
20584 KB |
Output is correct |
35 |
Correct |
156 ms |
20712 KB |
Output is correct |
36 |
Correct |
156 ms |
18864 KB |
Output is correct |
37 |
Correct |
151 ms |
16376 KB |
Output is correct |
38 |
Correct |
151 ms |
16064 KB |
Output is correct |
39 |
Correct |
160 ms |
16124 KB |
Output is correct |
40 |
Correct |
179 ms |
15992 KB |
Output is correct |
41 |
Correct |
198 ms |
15992 KB |
Output is correct |
42 |
Correct |
183 ms |
15992 KB |
Output is correct |
43 |
Correct |
154 ms |
18156 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
384 KB |
Output is correct |
52 |
Correct |
1 ms |
384 KB |
Output is correct |
53 |
Correct |
1 ms |
384 KB |
Output is correct |
54 |
Correct |
1 ms |
384 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
384 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
748 ms |
74604 KB |
Output is correct |
65 |
Correct |
802 ms |
74732 KB |
Output is correct |
66 |
Correct |
935 ms |
73832 KB |
Output is correct |
67 |
Correct |
871 ms |
73848 KB |
Output is correct |
68 |
Correct |
66 ms |
7672 KB |
Output is correct |
69 |
Correct |
72 ms |
7676 KB |
Output is correct |
70 |
Correct |
68 ms |
7792 KB |
Output is correct |
71 |
Correct |
67 ms |
7672 KB |
Output is correct |
72 |
Correct |
69 ms |
7784 KB |
Output is correct |
73 |
Correct |
75 ms |
7672 KB |
Output is correct |
74 |
Correct |
158 ms |
27368 KB |
Output is correct |
75 |
Correct |
155 ms |
27244 KB |
Output is correct |
76 |
Correct |
834 ms |
75680 KB |
Output is correct |
77 |
Correct |
814 ms |
75728 KB |
Output is correct |
78 |
Correct |
695 ms |
66788 KB |
Output is correct |
79 |
Correct |
732 ms |
67604 KB |
Output is correct |
80 |
Correct |
142 ms |
22648 KB |
Output is correct |
81 |
Correct |
1147 ms |
77664 KB |
Output is correct |
82 |
Correct |
1138 ms |
77668 KB |
Output is correct |
83 |
Correct |
937 ms |
75500 KB |
Output is correct |
84 |
Correct |
925 ms |
75240 KB |
Output is correct |
85 |
Correct |
906 ms |
74604 KB |
Output is correct |
86 |
Correct |
887 ms |
74096 KB |
Output is correct |
87 |
Correct |
994 ms |
74856 KB |
Output is correct |
88 |
Correct |
1000 ms |
76000 KB |
Output is correct |
89 |
Correct |
945 ms |
75892 KB |
Output is correct |
90 |
Correct |
875 ms |
73844 KB |
Output is correct |
91 |
Correct |
862 ms |
73720 KB |
Output is correct |
92 |
Correct |
923 ms |
73680 KB |
Output is correct |