Submission #138862

# Submission time Handle Problem Language Result Execution time Memory
138862 2019-07-30T14:43:30 Z win11905 Ancient Books (IOI17_books) C++11
100 / 100
234 ms 21568 KB
#include <bits/stdc++.h>
#include "books.h"
#define long long long 
using namespace std;
 
const int N = 1e6+5;
 
long ans = 0;
int n, x, y;
int id[N], L[N], R[N], ptr;
bool check[N];
 
void extend(int &l, int &r) {
	int ll = min(L[id[l]], L[id[r]]);
	int rr = max(R[id[l]], R[id[r]]);
	while(ll < l || r < rr) {
		if(ll < l) l--;
		else r++;
		ll = min(ll, min(L[id[l]], L[id[r]]));
		rr = max(rr, max(R[id[l]], R[id[r]]));
	}
}
 
long minimum_walk(vector<int> p, int s) {
	n = p.size();
	x = y = s;
	for(int i = 0; i < n; ++i) ans += abs(i - p[i]);
	for(int i = 0; i < n; ++i) if(!check[i]) {
		id[i] = ++ptr, check[i] = true;
		L[ptr] = R[ptr] = i;
		int u = i;
		while(p[u] != i) {
			id[u = p[u]] = ptr;
			check[u] = true;
			R[ptr] = max(R[ptr], u);
		}
		if(i != p[i]) x = min(x, L[ptr]), y = max(y, R[ptr]);
	}
	int l = s, r = s;
	while(x < l || r < y) {
		extend(l, r);
		int al = l, ar = r;
		int bl = l, br = r;
		long ca = 0, cb = 0;
		while(x < al && ar == r) ca += 2, extend(--al, ar);
		while(bl == l && br < y) cb += 2, extend(bl, ++br);
		if(ar != r && bl != l) ans += min(ca, cb);
		else ans += ca + cb;
		l = min(al, bl), r = max(ar, br);
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 234 ms 13008 KB Output is correct
31 Correct 215 ms 13048 KB Output is correct
32 Correct 145 ms 20844 KB Output is correct
33 Correct 158 ms 18552 KB Output is correct
34 Correct 167 ms 18512 KB Output is correct
35 Correct 177 ms 17344 KB Output is correct
36 Correct 157 ms 15044 KB Output is correct
37 Correct 150 ms 13304 KB Output is correct
38 Correct 155 ms 13176 KB Output is correct
39 Correct 156 ms 13176 KB Output is correct
40 Correct 167 ms 13152 KB Output is correct
41 Correct 188 ms 13028 KB Output is correct
42 Correct 184 ms 13048 KB Output is correct
43 Correct 157 ms 18788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 372 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 234 ms 13008 KB Output is correct
31 Correct 215 ms 13048 KB Output is correct
32 Correct 145 ms 20844 KB Output is correct
33 Correct 158 ms 18552 KB Output is correct
34 Correct 167 ms 18512 KB Output is correct
35 Correct 177 ms 17344 KB Output is correct
36 Correct 157 ms 15044 KB Output is correct
37 Correct 150 ms 13304 KB Output is correct
38 Correct 155 ms 13176 KB Output is correct
39 Correct 156 ms 13176 KB Output is correct
40 Correct 167 ms 13152 KB Output is correct
41 Correct 188 ms 13028 KB Output is correct
42 Correct 184 ms 13048 KB Output is correct
43 Correct 157 ms 18788 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 2 ms 376 KB Output is correct
49 Correct 2 ms 376 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 372 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 376 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 3 ms 376 KB Output is correct
63 Correct 2 ms 376 KB Output is correct
64 Correct 159 ms 19560 KB Output is correct
65 Correct 157 ms 20156 KB Output is correct
66 Correct 157 ms 14156 KB Output is correct
67 Correct 159 ms 14056 KB Output is correct
68 Correct 17 ms 2396 KB Output is correct
69 Correct 17 ms 2296 KB Output is correct
70 Correct 17 ms 2424 KB Output is correct
71 Correct 18 ms 2680 KB Output is correct
72 Correct 17 ms 2908 KB Output is correct
73 Correct 18 ms 2172 KB Output is correct
74 Correct 159 ms 19320 KB Output is correct
75 Correct 160 ms 19320 KB Output is correct
76 Correct 164 ms 19576 KB Output is correct
77 Correct 166 ms 19576 KB Output is correct
78 Correct 160 ms 18768 KB Output is correct
79 Correct 159 ms 18792 KB Output is correct
80 Correct 146 ms 21568 KB Output is correct
81 Correct 162 ms 17784 KB Output is correct
82 Correct 161 ms 17736 KB Output is correct
83 Correct 166 ms 16760 KB Output is correct
84 Correct 161 ms 16348 KB Output is correct
85 Correct 156 ms 14952 KB Output is correct
86 Correct 163 ms 14312 KB Output is correct
87 Correct 173 ms 20348 KB Output is correct
88 Correct 190 ms 19292 KB Output is correct
89 Correct 171 ms 17232 KB Output is correct
90 Correct 178 ms 13896 KB Output is correct
91 Correct 214 ms 13788 KB Output is correct
92 Correct 167 ms 13560 KB Output is correct