#include "books.h"
#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, s, val[maxn], nex, L[maxn], R[maxn], fr, to, prv[maxn], nxt[maxn];
ll base;
vector<int>p;
pii extend(int l, int r)
{
int cur_l = l, cur_r = r;
int i = l, j = r;
while(i >= cur_l || j <= cur_r)
{
if(i >= cur_l)
{
cur_l = min(cur_l, L[val[i]]);
cur_r = max(cur_r, R[val[i]]);
--i;
}
if(j <= cur_r)
{
cur_l = min(cur_l, L[val[j]]);
cur_r = max(cur_r, R[val[j]]);
j++;
}
}
return {cur_l, cur_r};
}
ll solve(int l, int r)
{
pii aa = extend(l, r);
int cur_l = aa.x, cur_r = aa.y;
if(cur_l <= fr && cur_r >= to)
return 0;
if(prv[cur_l] == -1)
{
if(cur_l > fr)
return 2 + solve(cur_l - 1, cur_r);
else
return 2 + solve(cur_l, cur_r + 1);
}
pii a = pii(cur_l, cur_r), b = pii(cur_l, cur_r);
int pr = prv[cur_l], nx = nxt[cur_r];
ll add1 = 0, add2 = 0;
while(a.x > pr)
a = extend(a.x - 1, a.y), add1 += 2;
while(b.y < nx)
b = extend(b.x, b.y + 1), add2 += 2;
if(add1 < add2)
return add1 + solve(a.x, a.y);
else
return add2 + solve(b.x, b.y);
}
ll minimum_walk(vector<int>perm, int S)
{
p = perm;
s = S;
n = sz(p);
for(int i = 1; i <= n; i++)
L[i] = mod, R[i] = -mod;
to = n - 1, fr = 0;
while(to >= 0 && p[to] == to)
--to;
while(fr < n && p[fr] == fr)
++fr;
if(fr > to)
return 0;
for(int i = 0; i < n; i++)
{
if(val[i] == 0)
{
int col = ++nex, cur = i;
while(val[cur] == 0)
{
val[cur] = col;
L[col] = min(L[col], cur);
R[col] = max(R[col], cur);
base += abs(p[cur] - cur);
cur = p[cur];
}
}
}
int pos = -1;
for(int i = 0; i <= s; i++)
{
prv[i] = pos;
if(R[val[i]] > s)
pos = i;
}
pos = -1;
for(int i = n - 1; i >= s; i--)
{
nxt[i] = pos;
if(L[val[i]] < s)
pos = i;
}
return base + solve(s, s);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
408 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
408 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
408 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
289 ms |
27732 KB |
Output is correct |
31 |
Correct |
340 ms |
27748 KB |
Output is correct |
32 |
Correct |
210 ms |
20088 KB |
Output is correct |
33 |
Correct |
166 ms |
27768 KB |
Output is correct |
34 |
Correct |
183 ms |
27768 KB |
Output is correct |
35 |
Correct |
156 ms |
27868 KB |
Output is correct |
36 |
Correct |
154 ms |
27740 KB |
Output is correct |
37 |
Correct |
165 ms |
27768 KB |
Output is correct |
38 |
Correct |
164 ms |
27756 KB |
Output is correct |
39 |
Correct |
166 ms |
27768 KB |
Output is correct |
40 |
Correct |
187 ms |
27740 KB |
Output is correct |
41 |
Correct |
258 ms |
27768 KB |
Output is correct |
42 |
Correct |
201 ms |
27868 KB |
Output is correct |
43 |
Correct |
162 ms |
27772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
356 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
412 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
408 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
289 ms |
27732 KB |
Output is correct |
31 |
Correct |
340 ms |
27748 KB |
Output is correct |
32 |
Correct |
210 ms |
20088 KB |
Output is correct |
33 |
Correct |
166 ms |
27768 KB |
Output is correct |
34 |
Correct |
183 ms |
27768 KB |
Output is correct |
35 |
Correct |
156 ms |
27868 KB |
Output is correct |
36 |
Correct |
154 ms |
27740 KB |
Output is correct |
37 |
Correct |
165 ms |
27768 KB |
Output is correct |
38 |
Correct |
164 ms |
27756 KB |
Output is correct |
39 |
Correct |
166 ms |
27768 KB |
Output is correct |
40 |
Correct |
187 ms |
27740 KB |
Output is correct |
41 |
Correct |
258 ms |
27768 KB |
Output is correct |
42 |
Correct |
201 ms |
27868 KB |
Output is correct |
43 |
Correct |
162 ms |
27772 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
2 ms |
384 KB |
Output is correct |
46 |
Correct |
2 ms |
356 KB |
Output is correct |
47 |
Correct |
2 ms |
384 KB |
Output is correct |
48 |
Correct |
3 ms |
384 KB |
Output is correct |
49 |
Correct |
3 ms |
412 KB |
Output is correct |
50 |
Correct |
2 ms |
384 KB |
Output is correct |
51 |
Correct |
3 ms |
384 KB |
Output is correct |
52 |
Correct |
2 ms |
384 KB |
Output is correct |
53 |
Correct |
3 ms |
384 KB |
Output is correct |
54 |
Correct |
3 ms |
384 KB |
Output is correct |
55 |
Correct |
2 ms |
384 KB |
Output is correct |
56 |
Correct |
2 ms |
384 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
3 ms |
384 KB |
Output is correct |
59 |
Correct |
3 ms |
384 KB |
Output is correct |
60 |
Correct |
2 ms |
384 KB |
Output is correct |
61 |
Correct |
2 ms |
384 KB |
Output is correct |
62 |
Correct |
3 ms |
384 KB |
Output is correct |
63 |
Correct |
2 ms |
384 KB |
Output is correct |
64 |
Correct |
131 ms |
27768 KB |
Output is correct |
65 |
Correct |
185 ms |
27896 KB |
Output is correct |
66 |
Correct |
189 ms |
27712 KB |
Output is correct |
67 |
Correct |
218 ms |
27768 KB |
Output is correct |
68 |
Correct |
18 ms |
3072 KB |
Output is correct |
69 |
Correct |
21 ms |
3192 KB |
Output is correct |
70 |
Correct |
16 ms |
3072 KB |
Output is correct |
71 |
Correct |
15 ms |
3200 KB |
Output is correct |
72 |
Correct |
17 ms |
3072 KB |
Output is correct |
73 |
Correct |
17 ms |
3200 KB |
Output is correct |
74 |
Correct |
185 ms |
27852 KB |
Output is correct |
75 |
Correct |
173 ms |
27772 KB |
Output is correct |
76 |
Correct |
153 ms |
27740 KB |
Output is correct |
77 |
Correct |
156 ms |
27748 KB |
Output is correct |
78 |
Correct |
142 ms |
27752 KB |
Output is correct |
79 |
Correct |
166 ms |
27772 KB |
Output is correct |
80 |
Correct |
130 ms |
19924 KB |
Output is correct |
81 |
Correct |
262 ms |
27768 KB |
Output is correct |
82 |
Correct |
168 ms |
27768 KB |
Output is correct |
83 |
Correct |
199 ms |
27772 KB |
Output is correct |
84 |
Correct |
175 ms |
27768 KB |
Output is correct |
85 |
Correct |
163 ms |
27768 KB |
Output is correct |
86 |
Correct |
148 ms |
27768 KB |
Output is correct |
87 |
Correct |
155 ms |
27768 KB |
Output is correct |
88 |
Correct |
167 ms |
27768 KB |
Output is correct |
89 |
Correct |
145 ms |
27768 KB |
Output is correct |
90 |
Correct |
140 ms |
27720 KB |
Output is correct |
91 |
Correct |
154 ms |
27712 KB |
Output is correct |
92 |
Correct |
165 ms |
27768 KB |
Output is correct |