# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
624662 |
2022-08-08T15:06:13 Z |
8e7 |
Ancient Books (IOI17_books) |
C++17 |
|
138 ms |
27768 KB |
//Challenge: Accepted
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ...b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 1000005
#define pii pair<int, int>
#define ff first
#define ss second
#include "books.h"
const int inf = 1e9;
pii ran[maxn], color[maxn];
long long minimum_walk(vector<int> p, int s) {
ll ans = 0;
int n = p.size();
vector<int> a(n, 0);
vector<bool> vis(n);
{
int c = 0;
for (int i = 0;i < n;i++) {
ans += abs(p[i] - i);
if (!vis[i]) {
c++;
int cur = i;
int mi = inf, ma = 0;
do {
mi = min(mi, cur);
ma = max(ma, cur);
a[cur] = c;
vis[cur] = 1;
cur = p[cur];
} while (!vis[cur]);
color[c] = {mi, ma};
}
}
for (int i = 0;i < n;i++) ran[i] = color[a[i]];
}
int lb = 0, rb = n-1;
while (lb < s && p[lb] == lb) lb++;
while (rb > s && p[rb] == rb) rb--;
auto expand = [&] (int l, int r, int nl, int nr, int type) { //0:none, 1:left, 2:right
if (type == 1) {
l = max(lb, l-1);
nl = min(nl, ran[l].ff);
nr = max(nr, ran[l].ss);
} else if(type == 2) {
r = min(rb, r+1);
nl = min(nl, ran[r].ff);
nr = max(nr, ran[r].ss);
}
while (nl < l || nr > r) {
while (nl < l) {
l--;
nl = min(nl, ran[l].ff);
nr = max(nr, ran[l].ss);
}
while (nr > r) {
r++;
nl = min(nl, ran[r].ff);
nr = max(nr, ran[r].ss);
}
}
return make_pair(l, r);
};
pii cur = {s, s};
cur = expand(s, s, ran[s].ff, ran[s].ss, 0);
while (cur.ff > lb || cur.ss < rb) {
if (cur.ff == lb) {
cur = expand(cur.ff, cur.ss, cur.ff, cur.ss, 2);
ans+=2;
} else if (cur.ss == rb){
cur = expand(cur.ff, cur.ss, cur.ff, cur.ss, 1);
ans+=2;
} else {
pii tmp = cur, lef;
int sl = 0, sr = 0, vl, vr;
while (tmp.ss <= cur.ss) {
tmp = expand(tmp.ff, tmp.ss, tmp.ff, tmp.ss, 1);
sl++;
if (tmp.ff == lb) break;
}
vl = sl;
if (tmp.ss <= cur.ss) vl = inf;
lef = tmp;
tmp = cur;
while (tmp.ff >= cur.ff) {
tmp = expand(tmp.ff, tmp.ss, tmp.ff, tmp.ss, 2);
sr++;
if (tmp.ss == rb || sr > vl) break;
}
vr = sr;
if (tmp.ff >= cur.ff) vr = inf;
if (vl == inf && vr == inf) {
ans += 2 * (sl + sr);
break;
} else if (vl < vr) {
ans += 2 * vl;
cur = lef;
} else {
ans += 2 * vr;
cur = tmp;
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
138 ms |
20088 KB |
Output is correct |
31 |
Correct |
134 ms |
19956 KB |
Output is correct |
32 |
Correct |
103 ms |
27724 KB |
Output is correct |
33 |
Correct |
105 ms |
25456 KB |
Output is correct |
34 |
Correct |
110 ms |
25384 KB |
Output is correct |
35 |
Correct |
107 ms |
24292 KB |
Output is correct |
36 |
Correct |
102 ms |
22004 KB |
Output is correct |
37 |
Correct |
96 ms |
20216 KB |
Output is correct |
38 |
Correct |
98 ms |
19972 KB |
Output is correct |
39 |
Correct |
98 ms |
19956 KB |
Output is correct |
40 |
Correct |
100 ms |
19968 KB |
Output is correct |
41 |
Correct |
112 ms |
20092 KB |
Output is correct |
42 |
Correct |
110 ms |
19960 KB |
Output is correct |
43 |
Correct |
104 ms |
25724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
216 KB |
Output is correct |
15 |
Correct |
1 ms |
216 KB |
Output is correct |
16 |
Correct |
0 ms |
216 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
138 ms |
20088 KB |
Output is correct |
31 |
Correct |
134 ms |
19956 KB |
Output is correct |
32 |
Correct |
103 ms |
27724 KB |
Output is correct |
33 |
Correct |
105 ms |
25456 KB |
Output is correct |
34 |
Correct |
110 ms |
25384 KB |
Output is correct |
35 |
Correct |
107 ms |
24292 KB |
Output is correct |
36 |
Correct |
102 ms |
22004 KB |
Output is correct |
37 |
Correct |
96 ms |
20216 KB |
Output is correct |
38 |
Correct |
98 ms |
19972 KB |
Output is correct |
39 |
Correct |
98 ms |
19956 KB |
Output is correct |
40 |
Correct |
100 ms |
19968 KB |
Output is correct |
41 |
Correct |
112 ms |
20092 KB |
Output is correct |
42 |
Correct |
110 ms |
19960 KB |
Output is correct |
43 |
Correct |
104 ms |
25724 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
212 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
0 ms |
216 KB |
Output is correct |
58 |
Correct |
1 ms |
216 KB |
Output is correct |
59 |
Correct |
0 ms |
216 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
0 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
108 ms |
25672 KB |
Output is correct |
65 |
Correct |
106 ms |
26228 KB |
Output is correct |
66 |
Correct |
105 ms |
20224 KB |
Output is correct |
67 |
Correct |
96 ms |
20084 KB |
Output is correct |
68 |
Correct |
9 ms |
2516 KB |
Output is correct |
69 |
Correct |
11 ms |
2372 KB |
Output is correct |
70 |
Correct |
10 ms |
2516 KB |
Output is correct |
71 |
Correct |
10 ms |
2772 KB |
Output is correct |
72 |
Correct |
10 ms |
2900 KB |
Output is correct |
73 |
Correct |
10 ms |
2260 KB |
Output is correct |
74 |
Correct |
108 ms |
25332 KB |
Output is correct |
75 |
Correct |
110 ms |
25432 KB |
Output is correct |
76 |
Correct |
104 ms |
25600 KB |
Output is correct |
77 |
Correct |
105 ms |
25732 KB |
Output is correct |
78 |
Correct |
103 ms |
24832 KB |
Output is correct |
79 |
Correct |
107 ms |
24836 KB |
Output is correct |
80 |
Correct |
104 ms |
27768 KB |
Output is correct |
81 |
Correct |
98 ms |
23796 KB |
Output is correct |
82 |
Correct |
98 ms |
23796 KB |
Output is correct |
83 |
Correct |
102 ms |
22988 KB |
Output is correct |
84 |
Correct |
103 ms |
22348 KB |
Output is correct |
85 |
Correct |
106 ms |
20980 KB |
Output is correct |
86 |
Correct |
96 ms |
20360 KB |
Output is correct |
87 |
Correct |
107 ms |
26496 KB |
Output is correct |
88 |
Correct |
104 ms |
25492 KB |
Output is correct |
89 |
Correct |
106 ms |
23544 KB |
Output is correct |
90 |
Correct |
96 ms |
20100 KB |
Output is correct |
91 |
Correct |
97 ms |
19956 KB |
Output is correct |
92 |
Correct |
99 ms |
20084 KB |
Output is correct |