Submission #442135

# Submission time Handle Problem Language Result Execution time Memory
442135 2021-07-07T07:52:01 Z prvocislo Ancient Books (IOI17_books) C++17
100 / 100
197 ms 26816 KB
#include "books.h"
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

const int maxn = 1e6 + 5;
vector<int> c(maxn, -1), L(maxn, maxn), R(maxn, -1);
void extend_for_free(int &l, int &r)
{
	int tl = min({l, L[c[l]], L[c[r]]}), tr = max({r, R[c[l]], R[c[r]]});
	while (tl < l || r < tr)
	{
		if (tl < l)
		{
			l--;
			tl = min(tl, L[c[l]]);
			tr = max(tr, R[c[l]]);
		}
		if (r < tr)
		{
			r++;
			tl = min(tl, L[c[r]]);
			tr = max(tr, R[c[r]]);
		}
	}
}
int calculate(int l, int r, int tl, int tr)
{
	int ans = 0;
	while (tl < l || r < tr)
	{
		extend_for_free(l, r);
		bool left = false, right = false;
		int cll = l, clr = r, crl = l, crr = r, costl = 0, costr = 0;
		while (tl < cll)
		{
			cll--, costl += 2;
			extend_for_free(cll, clr);
			if (clr > r) 
			{
				left = true; 
				break;
			}
		}
		while (tr > crr)
		{
			crr++, costr += 2;
			extend_for_free(crl, crr);
			if (crl < l) 
			{
				right = true; 
				break;
			}
		}
		if (left && right) ans += min(costl, costr);
		else ans += costl + costr;
		l = min(cll, crl), r = max(crl, crr);
	}
	return ans;
}
ll minimum_walk(vector<int> p, int s)
{
	int n = p.size(), num = -1, l = s, r = s;
	ll ans = 0;
	for (int i = 0; i < n; i++)
	{
		ans += abs(p[i] - i);
		if (c[i] != -1) continue;
		num++, L[num] = R[num] = i, c[i] = num;
		int j = p[i];
		while (j != i)
		{
			L[num] = min(L[num], j), R[num] = max(R[num], j), c[j] = num;
			j = p[j];
		}
		if (i != p[i]) l = min(l, L[num]), r = max(r, R[num]);
	}
	ans += calculate(s, s, l, r);
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 6 ms 11928 KB Output is correct
3 Correct 6 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
6 Correct 6 ms 11952 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 6 ms 12040 KB Output is correct
9 Correct 7 ms 11972 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 7 ms 11980 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 6 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 12028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 6 ms 11928 KB Output is correct
3 Correct 6 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
6 Correct 6 ms 11952 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 6 ms 12040 KB Output is correct
9 Correct 7 ms 11972 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 7 ms 11980 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 6 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 12028 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 6 ms 12048 KB Output is correct
21 Correct 7 ms 11980 KB Output is correct
22 Correct 8 ms 11936 KB Output is correct
23 Correct 7 ms 11980 KB Output is correct
24 Correct 7 ms 11980 KB Output is correct
25 Correct 7 ms 11980 KB Output is correct
26 Correct 7 ms 11980 KB Output is correct
27 Correct 7 ms 11980 KB Output is correct
28 Correct 7 ms 11980 KB Output is correct
29 Correct 7 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 6 ms 11928 KB Output is correct
3 Correct 6 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
6 Correct 6 ms 11952 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 6 ms 12040 KB Output is correct
9 Correct 7 ms 11972 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 7 ms 11980 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 6 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 12028 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 6 ms 12048 KB Output is correct
21 Correct 7 ms 11980 KB Output is correct
22 Correct 8 ms 11936 KB Output is correct
23 Correct 7 ms 11980 KB Output is correct
24 Correct 7 ms 11980 KB Output is correct
25 Correct 7 ms 11980 KB Output is correct
26 Correct 7 ms 11980 KB Output is correct
27 Correct 7 ms 11980 KB Output is correct
28 Correct 7 ms 11980 KB Output is correct
29 Correct 7 ms 11980 KB Output is correct
30 Correct 197 ms 19800 KB Output is correct
31 Correct 182 ms 19816 KB Output is correct
32 Correct 126 ms 19856 KB Output is correct
33 Correct 139 ms 19780 KB Output is correct
34 Correct 140 ms 19780 KB Output is correct
35 Correct 142 ms 19792 KB Output is correct
36 Correct 136 ms 19776 KB Output is correct
37 Correct 134 ms 19788 KB Output is correct
38 Correct 133 ms 19792 KB Output is correct
39 Correct 137 ms 19780 KB Output is correct
40 Correct 145 ms 19908 KB Output is correct
41 Correct 157 ms 19788 KB Output is correct
42 Correct 148 ms 19796 KB Output is correct
43 Correct 135 ms 19780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 8 ms 11980 KB Output is correct
3 Correct 7 ms 11980 KB Output is correct
4 Correct 7 ms 11980 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
6 Correct 7 ms 11980 KB Output is correct
7 Correct 7 ms 12052 KB Output is correct
8 Correct 7 ms 11972 KB Output is correct
9 Correct 7 ms 12052 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 7 ms 11980 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 7 ms 11980 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 7 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 7 ms 12052 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 7 ms 11980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11980 KB Output is correct
2 Correct 6 ms 11928 KB Output is correct
3 Correct 6 ms 11980 KB Output is correct
4 Correct 6 ms 11980 KB Output is correct
5 Correct 7 ms 11980 KB Output is correct
6 Correct 6 ms 11952 KB Output is correct
7 Correct 7 ms 11980 KB Output is correct
8 Correct 6 ms 12040 KB Output is correct
9 Correct 7 ms 11972 KB Output is correct
10 Correct 7 ms 11980 KB Output is correct
11 Correct 6 ms 11980 KB Output is correct
12 Correct 7 ms 11980 KB Output is correct
13 Correct 7 ms 11980 KB Output is correct
14 Correct 7 ms 11980 KB Output is correct
15 Correct 7 ms 11980 KB Output is correct
16 Correct 6 ms 11980 KB Output is correct
17 Correct 7 ms 11980 KB Output is correct
18 Correct 6 ms 12028 KB Output is correct
19 Correct 7 ms 11980 KB Output is correct
20 Correct 6 ms 12048 KB Output is correct
21 Correct 7 ms 11980 KB Output is correct
22 Correct 8 ms 11936 KB Output is correct
23 Correct 7 ms 11980 KB Output is correct
24 Correct 7 ms 11980 KB Output is correct
25 Correct 7 ms 11980 KB Output is correct
26 Correct 7 ms 11980 KB Output is correct
27 Correct 7 ms 11980 KB Output is correct
28 Correct 7 ms 11980 KB Output is correct
29 Correct 7 ms 11980 KB Output is correct
30 Correct 197 ms 19800 KB Output is correct
31 Correct 182 ms 19816 KB Output is correct
32 Correct 126 ms 19856 KB Output is correct
33 Correct 139 ms 19780 KB Output is correct
34 Correct 140 ms 19780 KB Output is correct
35 Correct 142 ms 19792 KB Output is correct
36 Correct 136 ms 19776 KB Output is correct
37 Correct 134 ms 19788 KB Output is correct
38 Correct 133 ms 19792 KB Output is correct
39 Correct 137 ms 19780 KB Output is correct
40 Correct 145 ms 19908 KB Output is correct
41 Correct 157 ms 19788 KB Output is correct
42 Correct 148 ms 19796 KB Output is correct
43 Correct 135 ms 19780 KB Output is correct
44 Correct 7 ms 11980 KB Output is correct
45 Correct 8 ms 11980 KB Output is correct
46 Correct 7 ms 11980 KB Output is correct
47 Correct 7 ms 11980 KB Output is correct
48 Correct 7 ms 11980 KB Output is correct
49 Correct 7 ms 11980 KB Output is correct
50 Correct 7 ms 12052 KB Output is correct
51 Correct 7 ms 11972 KB Output is correct
52 Correct 7 ms 12052 KB Output is correct
53 Correct 7 ms 11980 KB Output is correct
54 Correct 7 ms 11980 KB Output is correct
55 Correct 7 ms 11980 KB Output is correct
56 Correct 7 ms 11980 KB Output is correct
57 Correct 7 ms 11980 KB Output is correct
58 Correct 7 ms 11980 KB Output is correct
59 Correct 7 ms 11980 KB Output is correct
60 Correct 7 ms 11980 KB Output is correct
61 Correct 7 ms 12052 KB Output is correct
62 Correct 7 ms 11980 KB Output is correct
63 Correct 7 ms 11980 KB Output is correct
64 Correct 135 ms 26568 KB Output is correct
65 Correct 137 ms 26560 KB Output is correct
66 Correct 140 ms 26560 KB Output is correct
67 Correct 148 ms 26564 KB Output is correct
68 Correct 19 ms 13388 KB Output is correct
69 Correct 20 ms 13404 KB Output is correct
70 Correct 19 ms 13396 KB Output is correct
71 Correct 20 ms 13400 KB Output is correct
72 Correct 18 ms 13320 KB Output is correct
73 Correct 20 ms 13316 KB Output is correct
74 Correct 138 ms 26564 KB Output is correct
75 Correct 142 ms 26564 KB Output is correct
76 Correct 141 ms 26564 KB Output is correct
77 Correct 137 ms 26564 KB Output is correct
78 Correct 137 ms 26556 KB Output is correct
79 Correct 141 ms 26688 KB Output is correct
80 Correct 129 ms 26560 KB Output is correct
81 Correct 143 ms 26564 KB Output is correct
82 Correct 142 ms 26564 KB Output is correct
83 Correct 145 ms 26560 KB Output is correct
84 Correct 140 ms 26564 KB Output is correct
85 Correct 140 ms 26564 KB Output is correct
86 Correct 140 ms 26688 KB Output is correct
87 Correct 148 ms 26748 KB Output is correct
88 Correct 146 ms 26564 KB Output is correct
89 Correct 143 ms 26564 KB Output is correct
90 Correct 137 ms 26684 KB Output is correct
91 Correct 140 ms 26816 KB Output is correct
92 Correct 140 ms 26556 KB Output is correct