Submission #415519

# Submission time Handle Problem Language Result Execution time Memory
415519 2021-06-01T07:57:13 Z SeDunion Ancient Books (IOI17_books) C++17
100 / 100
257 ms 73576 KB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
 
void extend(int &l, int &r, vector<int>&C, vector<int>&L, vector<int>&R) {
	int ll = l, rr = r;
  	for (int i = l ; i <= min(r, l + 10) ; ++ i) ll = min(ll, L[C[i]]), rr = max(rr, R[C[i]]);
 	for (int i = max(l, r - 10) ; i <= r ; ++ i) ll = min(ll, L[C[i]]), rr = max(rr, R[C[i]]);
	while (ll != l || rr != r) {
		while (ll < l) {
			--l;
			ll = min(ll, L[C[l]]);
			rr = max(rr, R[C[l]]);
		}
		while (rr > r) {
			++r;
			ll = min(ll, L[C[r]]);
			rr = max(rr, R[C[r]]);
		}
	}
}
 
ll solve(int l, int r, int needl, int needr, vector<int>&C, vector<int>&L, vector<int>&R) {
	extend(l, r, C, L, R);
	if (needl == l && r == needr) return 0ll;
	int l_l = l, r_l = r; ll c_l = 0;
	while (true) {
		if (l_l <= needl) break;
		if (r_l > r) break;
		c_l += 2;
		--l_l;
		extend(l_l, r_l, C, L, R);
	}
	int l_r = l, r_r = r; ll c_r = 0;
	while (true) {
		if (r_r >= needr) break;
		if (l_r < l) break;
		c_r += 2;
		++r_r;
		extend(l_r, r_r, C, L, R);
	}
	assert((l_r < l) == (r_l > r));
	ll res = 0;
	if (l_r < l) res = min(c_l, c_r);
	else res = c_l + c_r;
	int tl = min(l_l, l_r);
	int tr = max(r_l, r_r);
	res += solve(tl, tr, needl, needr, C, L, R);
	//cerr << l << " " << r << " " << needl << " " << needr << " " << res << "\n";
	return res;
}
 
ll minimum_walk(vector<int> p, int s) {
	int n = p.size();
	vector<int>C(n, -1), L(n), R(n);
	int l = s, r = s;
	ll essential = 0;
	for (int i = 0 ; i < n ; ++ i) {
		essential += abs(i - p[i]);
		if (C[i] == -1) {
			L[i] = i;
			R[i] = i;
			int j = i;
			do {
				R[i] = max(R[i], j);
				C[j] = i;
				j = p[j];
			} while (i != j);
		}
		if (p[i] != i) l = min(l, i), r = max(r, i);
	}
	return solve(s, s, l, r, C, L, R) + essential;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 2 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 2 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 232 ms 19868 KB Output is correct
31 Correct 257 ms 19856 KB Output is correct
32 Correct 124 ms 19816 KB Output is correct
33 Correct 167 ms 19852 KB Output is correct
34 Correct 184 ms 26604 KB Output is correct
35 Correct 178 ms 26604 KB Output is correct
36 Correct 150 ms 26584 KB Output is correct
37 Correct 147 ms 26592 KB Output is correct
38 Correct 146 ms 26596 KB Output is correct
39 Correct 142 ms 26592 KB Output is correct
40 Correct 145 ms 26596 KB Output is correct
41 Correct 158 ms 26692 KB Output is correct
42 Correct 170 ms 26592 KB Output is correct
43 Correct 138 ms 26564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 296 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 2 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 232 ms 19868 KB Output is correct
31 Correct 257 ms 19856 KB Output is correct
32 Correct 124 ms 19816 KB Output is correct
33 Correct 167 ms 19852 KB Output is correct
34 Correct 184 ms 26604 KB Output is correct
35 Correct 178 ms 26604 KB Output is correct
36 Correct 150 ms 26584 KB Output is correct
37 Correct 147 ms 26592 KB Output is correct
38 Correct 146 ms 26596 KB Output is correct
39 Correct 142 ms 26592 KB Output is correct
40 Correct 145 ms 26596 KB Output is correct
41 Correct 158 ms 26692 KB Output is correct
42 Correct 170 ms 26592 KB Output is correct
43 Correct 138 ms 26564 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 296 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 332 KB Output is correct
55 Correct 1 ms 332 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 2 ms 204 KB Output is correct
64 Correct 140 ms 26600 KB Output is correct
65 Correct 136 ms 26568 KB Output is correct
66 Correct 144 ms 26616 KB Output is correct
67 Correct 141 ms 26584 KB Output is correct
68 Correct 15 ms 2764 KB Output is correct
69 Correct 14 ms 2728 KB Output is correct
70 Correct 14 ms 2764 KB Output is correct
71 Correct 15 ms 2808 KB Output is correct
72 Correct 17 ms 2704 KB Output is correct
73 Correct 14 ms 2736 KB Output is correct
74 Correct 168 ms 26592 KB Output is correct
75 Correct 168 ms 26604 KB Output is correct
76 Correct 149 ms 26596 KB Output is correct
77 Correct 139 ms 26716 KB Output is correct
78 Correct 153 ms 26728 KB Output is correct
79 Correct 144 ms 26588 KB Output is correct
80 Correct 130 ms 26564 KB Output is correct
81 Correct 216 ms 73540 KB Output is correct
82 Correct 228 ms 73576 KB Output is correct
83 Correct 163 ms 30788 KB Output is correct
84 Correct 150 ms 29508 KB Output is correct
85 Correct 145 ms 27616 KB Output is correct
86 Correct 145 ms 26948 KB Output is correct
87 Correct 207 ms 48344 KB Output is correct
88 Correct 186 ms 39364 KB Output is correct
89 Correct 164 ms 32216 KB Output is correct
90 Correct 140 ms 26584 KB Output is correct
91 Correct 135 ms 26596 KB Output is correct
92 Correct 143 ms 26588 KB Output is correct