Submission #406889

# Submission time Handle Problem Language Result Execution time Memory
406889 2021-05-18T07:16:41 Z ja_kingy Ancient Books (IOI17_books) C++14
100 / 100
221 ms 34952 KB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> pii;

long long minimum_walk(vector<int> p, int s) {
	long long ans = 0;
	int n = p.size(), ccnt = 0;
	vector<int> mn(n), mx(n), cyc(n);
	vector<pii> evs;
	for (int i = 0; i < n; ++i) {
		ans += abs(i-p[i]);
		if (!cyc[i]) {
			ccnt++;
			cyc[i] = ccnt;
			mn[ccnt] = mx[ccnt] = i;
			for (int x = p[i]; x != i; x = p[x]) {
				cyc[x] = ccnt;
				mn[ccnt] = min(mn[ccnt], x);
				mx[ccnt] = max(mx[ccnt], x);
			}
			if (mn[ccnt] != mx[ccnt] || i == s) {
				evs.push_back({mn[ccnt], -1});
				evs.push_back({mx[ccnt], 1});
			}
		}
	}
	sort(evs.begin(), evs.end());
	int rcnt = 0, pv = -1, fst;
	for (pii ev: evs) {
		if (rcnt == 0 && ~pv) {
			fst = ev.first;
			ans += (ev.first - pv) * 2;
		}
		rcnt -= ev.second;
		pv = ev.first;
		if (rcnt == 0 && fst <= s && s <= pv) {
			//cerr << rcnt << ' ' << fst << ' ' << pv << endl;
			int cl = s, cr = s, l = mn[cyc[s]], r = mx[cyc[s]];
			for (;;) {
				// cerr << l << ' ' << r << endl;
				while (cl != l || cr != r) {
					if (cl != l) {
						cl--;
						l = min(l, mn[cyc[cl]]);
						r = max(r, mx[cyc[cl]]);
					} else {
						cr++;
						l = min(l, mn[cyc[cr]]);
						r = max(r, mx[cyc[cr]]);
					}
				}
				if (l == fst || r == pv) break;
				ans+=2;
				l--;
				r++;
			}
		}
	}
	return ans;
}

Compilation message

books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:54:5: warning: 'fst' may be used uninitialized in this function [-Wmaybe-uninitialized]
   54 |     if (l == fst || r == pv) break;
      |     ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 292 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 288 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 292 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 288 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 292 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 288 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 292 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 292 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 288 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 292 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 288 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 292 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 205 ms 26536 KB Output is correct
31 Correct 221 ms 26480 KB Output is correct
32 Correct 131 ms 26696 KB Output is correct
33 Correct 172 ms 30828 KB Output is correct
34 Correct 145 ms 30808 KB Output is correct
35 Correct 148 ms 30804 KB Output is correct
36 Correct 152 ms 30796 KB Output is correct
37 Correct 162 ms 27224 KB Output is correct
38 Correct 136 ms 26708 KB Output is correct
39 Correct 133 ms 26812 KB Output is correct
40 Correct 137 ms 26592 KB Output is correct
41 Correct 147 ms 26592 KB Output is correct
42 Correct 144 ms 26584 KB Output is correct
43 Correct 161 ms 30940 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 332 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 292 KB Output is correct
12 Correct 1 ms 300 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 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 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 292 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 288 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 292 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 288 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 292 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 205 ms 26536 KB Output is correct
31 Correct 221 ms 26480 KB Output is correct
32 Correct 131 ms 26696 KB Output is correct
33 Correct 172 ms 30828 KB Output is correct
34 Correct 145 ms 30808 KB Output is correct
35 Correct 148 ms 30804 KB Output is correct
36 Correct 152 ms 30796 KB Output is correct
37 Correct 162 ms 27224 KB Output is correct
38 Correct 136 ms 26708 KB Output is correct
39 Correct 133 ms 26812 KB Output is correct
40 Correct 137 ms 26592 KB Output is correct
41 Correct 147 ms 26592 KB Output is correct
42 Correct 144 ms 26584 KB Output is correct
43 Correct 161 ms 30940 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 204 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 332 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 292 KB Output is correct
55 Correct 1 ms 300 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 332 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 165 ms 28764 KB Output is correct
65 Correct 157 ms 28752 KB Output is correct
66 Correct 140 ms 27328 KB Output is correct
67 Correct 147 ms 26980 KB Output is correct
68 Correct 20 ms 3108 KB Output is correct
69 Correct 14 ms 3020 KB Output is correct
70 Correct 14 ms 3116 KB Output is correct
71 Correct 15 ms 3192 KB Output is correct
72 Correct 14 ms 3108 KB Output is correct
73 Correct 14 ms 2800 KB Output is correct
74 Correct 143 ms 30792 KB Output is correct
75 Correct 147 ms 30732 KB Output is correct
76 Correct 189 ms 30792 KB Output is correct
77 Correct 162 ms 30772 KB Output is correct
78 Correct 178 ms 34896 KB Output is correct
79 Correct 211 ms 34892 KB Output is correct
80 Correct 145 ms 26596 KB Output is correct
81 Correct 179 ms 34856 KB Output is correct
82 Correct 187 ms 34952 KB Output is correct
83 Correct 218 ms 30796 KB Output is correct
84 Correct 167 ms 30800 KB Output is correct
85 Correct 147 ms 28756 KB Output is correct
86 Correct 182 ms 27736 KB Output is correct
87 Correct 153 ms 30804 KB Output is correct
88 Correct 187 ms 34884 KB Output is correct
89 Correct 186 ms 34864 KB Output is correct
90 Correct 156 ms 27328 KB Output is correct
91 Correct 134 ms 26820 KB Output is correct
92 Correct 145 ms 26592 KB Output is correct