/*
ID: varunra2
LANG: C++
TASK: books
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int s;
int n;
int targl, targr;
int cycs = 0;
VI wcyc;
VI lcyc;
VI rcyc;
ll retgood = 0;
ll retbad = 0;
void init(VI& order) {
n = sz(order);
targl = s;
targr = s;
wcyc.assign(n, -1);
lcyc.resize(n);
rcyc.resize(n);
}
void extend(int& l, int& r) {
// can we move l or r?
int nl, nr;
nl = min(lcyc[wcyc[l]], lcyc[wcyc[r]]);
nr = max(rcyc[wcyc[l]], rcyc[wcyc[r]]);
while (nl < l or nr > r) {
if (l > nl) {
l--;
nl = min(nl, lcyc[wcyc[l]]);
nr = max(nr, rcyc[wcyc[l]]);
} else {
r++;
nl = min(nl, lcyc[wcyc[r]]);
nr = max(nr, rcyc[wcyc[r]]);
}
}
}
ll solve() {
int l = s, r = s;
do {
debug(l, r, targl, targr);
// debug(l, r, targl, targr);
extend(l, r);
// debug(l, r, targl, targr);
int costl = 0, costr = 0, ll = l, lr = r, rl = l, rr = r;
bool next1 = false;
bool next2 = false;
while (true) {
if (ll <= targl) break;
ll--;
extend(ll, lr);
costl += 2;
if (lr > r) {
next1 = true;
break;
}
}
while (true) {
if (rr >= targr) break;
rr++;
extend(rl, rr);
costr += 2;
if (rl < l) {
next2 = true;
break;
}
}
assert(next1 == next2);
if (next1 and next2)
retbad += min(costl, costr);
else
retbad += (costl + costr);
l = min(ll, rl);
r = max(rr, lr);
debug(l, r, targl, targr);
debug(costl, costr);
} while (l > targl or r < targr);
debug(retbad);
return retbad;
}
ll minimum_walk(VI order, int s) {
::s = s;
init(order);
for (int i = 0; i < n; i++) {
retgood += abs(i - order[i]);
if (wcyc[i] != -1) continue;
int pos = i;
lcyc[cycs] = i;
rcyc[cycs] = i;
do {
wcyc[pos] = cycs;
rcyc[cycs] = max(rcyc[cycs], pos);
pos = order[pos];
} while (pos != i);
if (i != order[i]) {
targl = min(targl, lcyc[cycs]);
targr = max(targr, rcyc[cycs]);
}
cycs++;
}
debug(retgood);
return retgood + solve();
}
// int main() {
// #ifndef ONLINE_JUDGE
// freopen("books.in", "r", stdin);
// freopen("books.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0);
// cin.tie(0);
// int n, s;
// cin >> n >> s;
// VI order(n);
// for (int i = 0; i < n; i++) {
// cin >> order[i];
// }
// cout << minimum_walk(order, s) << '\n';
// return 0;
// }
Compilation message
books.cpp: In function 'll solve()':
books.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
books.cpp:96:5: note: in expansion of macro 'debug'
96 | debug(l, r, targl, targr);
| ^~~~~
books.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
books.cpp:135:5: note: in expansion of macro 'debug'
135 | debug(l, r, targl, targr);
| ^~~~~
books.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
books.cpp:136:5: note: in expansion of macro 'debug'
136 | debug(costl, costr);
| ^~~~~
books.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
books.cpp:138:3: note: in expansion of macro 'debug'
138 | debug(retbad);
| ^~~~~
books.cpp: In function 'll minimum_walk(VI, int)':
books.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
books.cpp:162:3: note: in expansion of macro 'debug'
162 | debug(retgood);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
225 ms |
19940 KB |
Output is correct |
31 |
Correct |
224 ms |
20036 KB |
Output is correct |
32 |
Correct |
152 ms |
19940 KB |
Output is correct |
33 |
Correct |
164 ms |
19928 KB |
Output is correct |
34 |
Correct |
164 ms |
19940 KB |
Output is correct |
35 |
Correct |
167 ms |
19940 KB |
Output is correct |
36 |
Correct |
154 ms |
19940 KB |
Output is correct |
37 |
Correct |
149 ms |
19940 KB |
Output is correct |
38 |
Correct |
148 ms |
19940 KB |
Output is correct |
39 |
Correct |
150 ms |
19944 KB |
Output is correct |
40 |
Correct |
160 ms |
19940 KB |
Output is correct |
41 |
Correct |
172 ms |
19940 KB |
Output is correct |
42 |
Correct |
166 ms |
19936 KB |
Output is correct |
43 |
Correct |
157 ms |
19940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
225 ms |
19940 KB |
Output is correct |
31 |
Correct |
224 ms |
20036 KB |
Output is correct |
32 |
Correct |
152 ms |
19940 KB |
Output is correct |
33 |
Correct |
164 ms |
19928 KB |
Output is correct |
34 |
Correct |
164 ms |
19940 KB |
Output is correct |
35 |
Correct |
167 ms |
19940 KB |
Output is correct |
36 |
Correct |
154 ms |
19940 KB |
Output is correct |
37 |
Correct |
149 ms |
19940 KB |
Output is correct |
38 |
Correct |
148 ms |
19940 KB |
Output is correct |
39 |
Correct |
150 ms |
19944 KB |
Output is correct |
40 |
Correct |
160 ms |
19940 KB |
Output is correct |
41 |
Correct |
172 ms |
19940 KB |
Output is correct |
42 |
Correct |
166 ms |
19936 KB |
Output is correct |
43 |
Correct |
157 ms |
19940 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
364 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
170 ms |
26616 KB |
Output is correct |
65 |
Correct |
158 ms |
26596 KB |
Output is correct |
66 |
Correct |
154 ms |
26596 KB |
Output is correct |
67 |
Correct |
169 ms |
26724 KB |
Output is correct |
68 |
Correct |
15 ms |
2924 KB |
Output is correct |
69 |
Correct |
17 ms |
3052 KB |
Output is correct |
70 |
Correct |
15 ms |
2924 KB |
Output is correct |
71 |
Correct |
16 ms |
2924 KB |
Output is correct |
72 |
Correct |
19 ms |
2924 KB |
Output is correct |
73 |
Correct |
17 ms |
2924 KB |
Output is correct |
74 |
Correct |
162 ms |
26596 KB |
Output is correct |
75 |
Correct |
171 ms |
26596 KB |
Output is correct |
76 |
Correct |
161 ms |
26596 KB |
Output is correct |
77 |
Correct |
163 ms |
26596 KB |
Output is correct |
78 |
Correct |
159 ms |
26596 KB |
Output is correct |
79 |
Correct |
166 ms |
26596 KB |
Output is correct |
80 |
Correct |
156 ms |
26596 KB |
Output is correct |
81 |
Correct |
167 ms |
26724 KB |
Output is correct |
82 |
Correct |
162 ms |
26596 KB |
Output is correct |
83 |
Correct |
161 ms |
26724 KB |
Output is correct |
84 |
Correct |
160 ms |
26724 KB |
Output is correct |
85 |
Correct |
162 ms |
26720 KB |
Output is correct |
86 |
Correct |
153 ms |
26596 KB |
Output is correct |
87 |
Correct |
169 ms |
26596 KB |
Output is correct |
88 |
Correct |
169 ms |
26596 KB |
Output is correct |
89 |
Correct |
164 ms |
26596 KB |
Output is correct |
90 |
Correct |
157 ms |
26724 KB |
Output is correct |
91 |
Correct |
161 ms |
26724 KB |
Output is correct |
92 |
Correct |
160 ms |
26596 KB |
Output is correct |