Submission #342751

# Submission time Handle Problem Language Result Execution time Memory
342751 2021-01-02T17:41:30 Z cki86201 Ancient Books (IOI17_books) C++11
100 / 100
350 ms 38772 KB
#include <bits/stdc++.h>
#include "books.h"
using namespace std;

#define Fi first
#define Se second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define szz(x) (int)(x).size()
#define rep(i, n) for(int i=0;i<(n);i++)
typedef tuple<int, int, int> t3;

int c[1000010], rrv[1000010], rv[1000010], T[1000010], S[1000010];
ll minimum_walk(vector<int> p, int s) {
	int n = szz(p);
	ll ans = 0;
	rep(i, n) {
		int l = i, r = p[i];
		if(l > r) swap(l, r);
		T[l]++, T[r]--;
		ans += r - l;
	}
	for(int i=1;i<n;i++) T[i] += T[i-1];
	rep(i, n) S[i] = (i ? S[i-1] : 0) + (T[i] == 0);
	auto gs = [&](int s, int e) {
		return (e ? S[e-1] : 0) - (s ? S[s-1] : 0);
	};
	rep(i, n) if(!c[i]) {
		int l = i, r = i;
		for(int j=p[i];j!=i;j=p[j]) l = min(l, j), r = max(r, j);
		c[i] = 1, rv[i] = r;
		for(int j=p[i];j!=i;j=p[j]) c[j] = 1, rv[j] = r;
	}
	memset(c, 0, sizeof c);
	rrv[0] = rv[0];
	for(int i=1;i<n;i++) rrv[i] = max(rv[i], rrv[i-1]);
	int l = s, r = s;
	vector <int> q; q.pb(s);
	auto push = [&](int x) {
		if(l > x) {
			for(int a=l-1;a>=x;a--) q.pb(a);
			l = x;
		}
		if(r < x) {
			for(int a=r+1;a<=x;a++) q.pb(a);
			r = x;
		}
	};
	while(1) {
		rep(i, szz(q)) {
			int t = q[i];
			if(c[t]) continue;
			c[t] = 1;
			for(int x=p[t];x!=t;x=p[x]) {
				push(x);
				c[x] = 1;
			}
		}
		if(l == 0 || rrv[l-1] <= r) break;
		q.clear();
		ans += 2;
		if(l) push(l-1);
		if(r < n-1) push(r+1);
	}
	int L = 0, R = n - 1;
	while(L < l && p[L] == L) ++L;
	while(r < R && p[R] == R) --R;
	ans += 2 * (gs(L, l) + gs(r, R));
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4204 KB Output is correct
2 Correct 3 ms 4204 KB Output is correct
3 Correct 3 ms 4204 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 2 ms 4204 KB Output is correct
7 Correct 2 ms 4204 KB Output is correct
8 Correct 2 ms 4204 KB Output is correct
9 Correct 2 ms 4204 KB Output is correct
10 Correct 3 ms 4204 KB Output is correct
11 Correct 3 ms 4236 KB Output is correct
12 Correct 3 ms 4204 KB Output is correct
13 Correct 2 ms 4204 KB Output is correct
14 Correct 2 ms 4204 KB Output is correct
15 Correct 3 ms 4204 KB Output is correct
16 Correct 3 ms 4204 KB Output is correct
17 Correct 3 ms 4204 KB Output is correct
18 Correct 3 ms 4204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4204 KB Output is correct
2 Correct 3 ms 4204 KB Output is correct
3 Correct 3 ms 4204 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 2 ms 4204 KB Output is correct
7 Correct 2 ms 4204 KB Output is correct
8 Correct 2 ms 4204 KB Output is correct
9 Correct 2 ms 4204 KB Output is correct
10 Correct 3 ms 4204 KB Output is correct
11 Correct 3 ms 4236 KB Output is correct
12 Correct 3 ms 4204 KB Output is correct
13 Correct 2 ms 4204 KB Output is correct
14 Correct 2 ms 4204 KB Output is correct
15 Correct 3 ms 4204 KB Output is correct
16 Correct 3 ms 4204 KB Output is correct
17 Correct 3 ms 4204 KB Output is correct
18 Correct 3 ms 4204 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
21 Correct 3 ms 4332 KB Output is correct
22 Correct 3 ms 4224 KB Output is correct
23 Correct 3 ms 4332 KB Output is correct
24 Correct 3 ms 4332 KB Output is correct
25 Correct 3 ms 4332 KB Output is correct
26 Correct 3 ms 4332 KB Output is correct
27 Correct 3 ms 4332 KB Output is correct
28 Correct 3 ms 4332 KB Output is correct
29 Correct 3 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4204 KB Output is correct
2 Correct 3 ms 4204 KB Output is correct
3 Correct 3 ms 4204 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 2 ms 4204 KB Output is correct
7 Correct 2 ms 4204 KB Output is correct
8 Correct 2 ms 4204 KB Output is correct
9 Correct 2 ms 4204 KB Output is correct
10 Correct 3 ms 4204 KB Output is correct
11 Correct 3 ms 4236 KB Output is correct
12 Correct 3 ms 4204 KB Output is correct
13 Correct 2 ms 4204 KB Output is correct
14 Correct 2 ms 4204 KB Output is correct
15 Correct 3 ms 4204 KB Output is correct
16 Correct 3 ms 4204 KB Output is correct
17 Correct 3 ms 4204 KB Output is correct
18 Correct 3 ms 4204 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
21 Correct 3 ms 4332 KB Output is correct
22 Correct 3 ms 4224 KB Output is correct
23 Correct 3 ms 4332 KB Output is correct
24 Correct 3 ms 4332 KB Output is correct
25 Correct 3 ms 4332 KB Output is correct
26 Correct 3 ms 4332 KB Output is correct
27 Correct 3 ms 4332 KB Output is correct
28 Correct 3 ms 4332 KB Output is correct
29 Correct 3 ms 4332 KB Output is correct
30 Correct 350 ms 31964 KB Output is correct
31 Correct 348 ms 31964 KB Output is correct
32 Correct 148 ms 27756 KB Output is correct
33 Correct 157 ms 27756 KB Output is correct
34 Correct 157 ms 27756 KB Output is correct
35 Correct 169 ms 27904 KB Output is correct
36 Correct 171 ms 27756 KB Output is correct
37 Correct 156 ms 27756 KB Output is correct
38 Correct 155 ms 27756 KB Output is correct
39 Correct 156 ms 27756 KB Output is correct
40 Correct 183 ms 27884 KB Output is correct
41 Correct 233 ms 29028 KB Output is correct
42 Correct 227 ms 27756 KB Output is correct
43 Correct 190 ms 31964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4332 KB Output is correct
2 Correct 4 ms 4332 KB Output is correct
3 Correct 4 ms 4348 KB Output is correct
4 Correct 3 ms 4332 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 4 ms 4332 KB Output is correct
7 Correct 3 ms 4332 KB Output is correct
8 Correct 3 ms 4332 KB Output is correct
9 Correct 3 ms 4332 KB Output is correct
10 Correct 4 ms 4332 KB Output is correct
11 Correct 3 ms 4332 KB Output is correct
12 Correct 3 ms 4336 KB Output is correct
13 Correct 3 ms 4332 KB Output is correct
14 Correct 3 ms 4332 KB Output is correct
15 Correct 3 ms 4332 KB Output is correct
16 Correct 3 ms 4332 KB Output is correct
17 Correct 3 ms 4332 KB Output is correct
18 Correct 3 ms 4332 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4204 KB Output is correct
2 Correct 3 ms 4204 KB Output is correct
3 Correct 3 ms 4204 KB Output is correct
4 Correct 3 ms 4224 KB Output is correct
5 Correct 3 ms 4332 KB Output is correct
6 Correct 2 ms 4204 KB Output is correct
7 Correct 2 ms 4204 KB Output is correct
8 Correct 2 ms 4204 KB Output is correct
9 Correct 2 ms 4204 KB Output is correct
10 Correct 3 ms 4204 KB Output is correct
11 Correct 3 ms 4236 KB Output is correct
12 Correct 3 ms 4204 KB Output is correct
13 Correct 2 ms 4204 KB Output is correct
14 Correct 2 ms 4204 KB Output is correct
15 Correct 3 ms 4204 KB Output is correct
16 Correct 3 ms 4204 KB Output is correct
17 Correct 3 ms 4204 KB Output is correct
18 Correct 3 ms 4204 KB Output is correct
19 Correct 3 ms 4332 KB Output is correct
20 Correct 3 ms 4332 KB Output is correct
21 Correct 3 ms 4332 KB Output is correct
22 Correct 3 ms 4224 KB Output is correct
23 Correct 3 ms 4332 KB Output is correct
24 Correct 3 ms 4332 KB Output is correct
25 Correct 3 ms 4332 KB Output is correct
26 Correct 3 ms 4332 KB Output is correct
27 Correct 3 ms 4332 KB Output is correct
28 Correct 3 ms 4332 KB Output is correct
29 Correct 3 ms 4332 KB Output is correct
30 Correct 350 ms 31964 KB Output is correct
31 Correct 348 ms 31964 KB Output is correct
32 Correct 148 ms 27756 KB Output is correct
33 Correct 157 ms 27756 KB Output is correct
34 Correct 157 ms 27756 KB Output is correct
35 Correct 169 ms 27904 KB Output is correct
36 Correct 171 ms 27756 KB Output is correct
37 Correct 156 ms 27756 KB Output is correct
38 Correct 155 ms 27756 KB Output is correct
39 Correct 156 ms 27756 KB Output is correct
40 Correct 183 ms 27884 KB Output is correct
41 Correct 233 ms 29028 KB Output is correct
42 Correct 227 ms 27756 KB Output is correct
43 Correct 190 ms 31964 KB Output is correct
44 Correct 3 ms 4332 KB Output is correct
45 Correct 4 ms 4332 KB Output is correct
46 Correct 4 ms 4348 KB Output is correct
47 Correct 3 ms 4332 KB Output is correct
48 Correct 3 ms 4332 KB Output is correct
49 Correct 4 ms 4332 KB Output is correct
50 Correct 3 ms 4332 KB Output is correct
51 Correct 3 ms 4332 KB Output is correct
52 Correct 3 ms 4332 KB Output is correct
53 Correct 4 ms 4332 KB Output is correct
54 Correct 3 ms 4332 KB Output is correct
55 Correct 3 ms 4336 KB Output is correct
56 Correct 3 ms 4332 KB Output is correct
57 Correct 3 ms 4332 KB Output is correct
58 Correct 3 ms 4332 KB Output is correct
59 Correct 3 ms 4332 KB Output is correct
60 Correct 3 ms 4332 KB Output is correct
61 Correct 3 ms 4332 KB Output is correct
62 Correct 3 ms 4332 KB Output is correct
63 Correct 3 ms 4332 KB Output is correct
64 Correct 176 ms 38748 KB Output is correct
65 Correct 190 ms 38708 KB Output is correct
66 Correct 181 ms 35684 KB Output is correct
67 Correct 175 ms 38748 KB Output is correct
68 Correct 20 ms 7784 KB Output is correct
69 Correct 19 ms 7404 KB Output is correct
70 Correct 19 ms 7784 KB Output is correct
71 Correct 21 ms 7784 KB Output is correct
72 Correct 19 ms 7404 KB Output is correct
73 Correct 20 ms 7532 KB Output is correct
74 Correct 180 ms 34500 KB Output is correct
75 Correct 160 ms 34540 KB Output is correct
76 Correct 182 ms 38772 KB Output is correct
77 Correct 180 ms 38748 KB Output is correct
78 Correct 179 ms 36960 KB Output is correct
79 Correct 179 ms 35644 KB Output is correct
80 Correct 159 ms 34412 KB Output is correct
81 Correct 159 ms 35264 KB Output is correct
82 Correct 160 ms 34924 KB Output is correct
83 Correct 185 ms 34540 KB Output is correct
84 Correct 186 ms 34412 KB Output is correct
85 Correct 177 ms 34412 KB Output is correct
86 Correct 173 ms 34540 KB Output is correct
87 Correct 168 ms 34412 KB Output is correct
88 Correct 172 ms 34500 KB Output is correct
89 Correct 182 ms 34540 KB Output is correct
90 Correct 171 ms 34560 KB Output is correct
91 Correct 173 ms 34540 KB Output is correct
92 Correct 185 ms 34924 KB Output is correct