// Source: https://oj.uz/problem/view/IOI17_books
//
#include "books.h"
#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<int> vi;
#define FOR(i, a, b) for (int i = (a); i<b; i++)
bool ckmin(int& a, int b){ return b < a ? a = b, true : false; }
bool ckmax(int& a, int b){ return b > a ? a = b, true : false; }
const int N = 1e6 + 10;
int ri[N], le[N];
bool vis[N];
ll minimum_walk(vector<int> p, int s) {
if (s == 0) {
ll ans = 0;
int n = p.size();
FOR(i, 0, n) {
if (!vis[i]) {
int l, r;
r = l = i;
int cur = p[i];
while (cur != i) {
l = min(l, cur);r=max(r, cur);
cur=p[cur];
}
cur = i;
do {
ans += abs(p[cur]-cur);
le[cur] = l;
ri[cur] = r;
vis[cur]=true;
cur=p[cur];
} while (cur != i);
}
}
int en = n-1;
while (en != -1 && p[en] == en) en--;
int r =0;
FOR(i, 0, en+1) {
if (r < i) {
r++;
ans+=2;
}
r = max(p[i], r);
}
return ans;
} else assert(false);
}
// int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// int res = minimum_walk({0, 2, 3, 1, 5, 6, 4}, 0);
// cout << res << endl;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4556 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4692 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4540 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4692 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4556 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4692 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4540 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4692 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4556 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4692 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4540 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4692 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
108 ms |
23552 KB |
Output is correct |
31 |
Correct |
113 ms |
23664 KB |
Output is correct |
32 |
Correct |
77 ms |
23636 KB |
Output is correct |
33 |
Correct |
82 ms |
23548 KB |
Output is correct |
34 |
Correct |
82 ms |
23620 KB |
Output is correct |
35 |
Correct |
90 ms |
24144 KB |
Output is correct |
36 |
Correct |
85 ms |
23664 KB |
Output is correct |
37 |
Correct |
81 ms |
23676 KB |
Output is correct |
38 |
Correct |
81 ms |
23636 KB |
Output is correct |
39 |
Correct |
77 ms |
23676 KB |
Output is correct |
40 |
Correct |
86 ms |
23636 KB |
Output is correct |
41 |
Correct |
98 ms |
23636 KB |
Output is correct |
42 |
Correct |
95 ms |
23624 KB |
Output is correct |
43 |
Correct |
81 ms |
23544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4556 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4692 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4540 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4692 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
108 ms |
23552 KB |
Output is correct |
31 |
Correct |
113 ms |
23664 KB |
Output is correct |
32 |
Correct |
77 ms |
23636 KB |
Output is correct |
33 |
Correct |
82 ms |
23548 KB |
Output is correct |
34 |
Correct |
82 ms |
23620 KB |
Output is correct |
35 |
Correct |
90 ms |
24144 KB |
Output is correct |
36 |
Correct |
85 ms |
23664 KB |
Output is correct |
37 |
Correct |
81 ms |
23676 KB |
Output is correct |
38 |
Correct |
81 ms |
23636 KB |
Output is correct |
39 |
Correct |
77 ms |
23676 KB |
Output is correct |
40 |
Correct |
86 ms |
23636 KB |
Output is correct |
41 |
Correct |
98 ms |
23636 KB |
Output is correct |
42 |
Correct |
95 ms |
23624 KB |
Output is correct |
43 |
Correct |
81 ms |
23544 KB |
Output is correct |
44 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 6 |
45 |
Halted |
0 ms |
0 KB |
- |