#include "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e9+7;
const int LIM=1e6+7;
ll F[LIM], mi[LIM], ma[LIM], l, r;
set<ll>S;
ll fnd(ll x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
void uni(int a, int b) {
if(fnd(a)==fnd(b)) return;
ma[fnd(a)]=max(ma[fnd(a)], ma[fnd(b)]);
mi[fnd(a)]=min(mi[fnd(a)], mi[fnd(b)]);
F[fnd(b)]=fnd(a);
}
ll solve(ll aktl, ll aktr, ll aktmi, ll aktma) {
if(aktl>aktmi) {
--aktl;
aktmi=min(aktmi, mi[fnd(aktl)]);
aktma=max(aktma, ma[fnd(aktl)]);
return solve(aktl, aktr, aktmi, aktma);
}
if(aktr<aktma) {
++aktr;
aktmi=min(aktmi, mi[fnd(aktr)]);
aktma=max(aktma, ma[fnd(aktr)]);
return solve(aktl, aktr, aktmi, aktma);
}
if(aktl==l && aktr==r) return 0;
ll xl=aktl, xr=aktr, xmi=aktmi, xma=aktma, xans=0;
while(xl>l) {
--xl;
if(xl<xmi) xans+=2;
xmi=min(xmi, mi[fnd(xl)]);
xma=max(xma, ma[fnd(xl)]);
if(xma>xr) break;
}
if(xma==xr && aktl!=l) return solve(xl, xr, xmi, xma)+xans;
ll yl=aktl, yr=aktr, ymi=aktmi, yma=aktma, yans=0;
while(yr<r) {
++yr;
if(yr>yma) yans+=2;
ymi=min(ymi, mi[fnd(yr)]);
yma=max(yma, ma[fnd(yr)]);
if(ymi<yl) break;
}
if(ymi==yl && aktr!=r) return solve(yl, yr, ymi, yma)+yans;
return solve(yl, yr, ymi, yma)+min(xans, yans);
}
ll minimum_walk(vector<int>p, int s) {
ll n=p.size();
rep(i, n) {
F[i]=ma[i]=mi[i]=i;
S.insert(i);
}
rep(i, n) uni(i, p[i]);
vector<pair<pair<ll,ll>,pair<ll,ll>>>V;
l=0; r=n-1;
while(l<s && p[l]==l) ++l;
while(r>s && p[r]==r) --r;
ll ans=0;
rep(i, n) ans+=abs(p[i]-i);
return ans+solve(s, s, mi[fnd(s)], ma[fnd(s)]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4448 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4600 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4448 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4600 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4448 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4600 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
417 ms |
78600 KB |
Output is correct |
31 |
Correct |
394 ms |
85512 KB |
Output is correct |
32 |
Correct |
270 ms |
85328 KB |
Output is correct |
33 |
Correct |
283 ms |
85388 KB |
Output is correct |
34 |
Correct |
275 ms |
85288 KB |
Output is correct |
35 |
Correct |
272 ms |
85356 KB |
Output is correct |
36 |
Correct |
298 ms |
85328 KB |
Output is correct |
37 |
Correct |
301 ms |
85380 KB |
Output is correct |
38 |
Correct |
279 ms |
85332 KB |
Output is correct |
39 |
Correct |
322 ms |
85328 KB |
Output is correct |
40 |
Correct |
285 ms |
85288 KB |
Output is correct |
41 |
Correct |
377 ms |
85440 KB |
Output is correct |
42 |
Correct |
303 ms |
85360 KB |
Output is correct |
43 |
Correct |
325 ms |
85224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4696 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
2 ms |
4448 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
3 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4600 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4444 KB |
Output is correct |
30 |
Correct |
417 ms |
78600 KB |
Output is correct |
31 |
Correct |
394 ms |
85512 KB |
Output is correct |
32 |
Correct |
270 ms |
85328 KB |
Output is correct |
33 |
Correct |
283 ms |
85388 KB |
Output is correct |
34 |
Correct |
275 ms |
85288 KB |
Output is correct |
35 |
Correct |
272 ms |
85356 KB |
Output is correct |
36 |
Correct |
298 ms |
85328 KB |
Output is correct |
37 |
Correct |
301 ms |
85380 KB |
Output is correct |
38 |
Correct |
279 ms |
85332 KB |
Output is correct |
39 |
Correct |
322 ms |
85328 KB |
Output is correct |
40 |
Correct |
285 ms |
85288 KB |
Output is correct |
41 |
Correct |
377 ms |
85440 KB |
Output is correct |
42 |
Correct |
303 ms |
85360 KB |
Output is correct |
43 |
Correct |
325 ms |
85224 KB |
Output is correct |
44 |
Correct |
2 ms |
4444 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4444 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
2 ms |
4444 KB |
Output is correct |
50 |
Correct |
1 ms |
4444 KB |
Output is correct |
51 |
Correct |
1 ms |
4444 KB |
Output is correct |
52 |
Correct |
2 ms |
4440 KB |
Output is correct |
53 |
Correct |
1 ms |
4444 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4444 KB |
Output is correct |
56 |
Correct |
2 ms |
4440 KB |
Output is correct |
57 |
Correct |
1 ms |
4444 KB |
Output is correct |
58 |
Correct |
1 ms |
4444 KB |
Output is correct |
59 |
Correct |
1 ms |
4444 KB |
Output is correct |
60 |
Correct |
1 ms |
4444 KB |
Output is correct |
61 |
Correct |
1 ms |
4444 KB |
Output is correct |
62 |
Correct |
1 ms |
4696 KB |
Output is correct |
63 |
Correct |
1 ms |
4440 KB |
Output is correct |
64 |
Correct |
282 ms |
85228 KB |
Output is correct |
65 |
Correct |
285 ms |
85356 KB |
Output is correct |
66 |
Correct |
328 ms |
85236 KB |
Output is correct |
67 |
Correct |
301 ms |
85372 KB |
Output is correct |
68 |
Correct |
26 ms |
14676 KB |
Output is correct |
69 |
Correct |
26 ms |
14680 KB |
Output is correct |
70 |
Correct |
26 ms |
14684 KB |
Output is correct |
71 |
Correct |
26 ms |
14676 KB |
Output is correct |
72 |
Correct |
24 ms |
14684 KB |
Output is correct |
73 |
Correct |
27 ms |
14680 KB |
Output is correct |
74 |
Correct |
276 ms |
85336 KB |
Output is correct |
75 |
Correct |
289 ms |
85360 KB |
Output is correct |
76 |
Correct |
281 ms |
85460 KB |
Output is correct |
77 |
Correct |
276 ms |
85328 KB |
Output is correct |
78 |
Correct |
296 ms |
85352 KB |
Output is correct |
79 |
Correct |
291 ms |
85332 KB |
Output is correct |
80 |
Correct |
256 ms |
85428 KB |
Output is correct |
81 |
Correct |
273 ms |
85584 KB |
Output is correct |
82 |
Correct |
273 ms |
85232 KB |
Output is correct |
83 |
Correct |
289 ms |
85240 KB |
Output is correct |
84 |
Correct |
292 ms |
85332 KB |
Output is correct |
85 |
Correct |
295 ms |
85328 KB |
Output is correct |
86 |
Correct |
294 ms |
85352 KB |
Output is correct |
87 |
Correct |
284 ms |
85332 KB |
Output is correct |
88 |
Correct |
270 ms |
85328 KB |
Output is correct |
89 |
Correct |
293 ms |
85328 KB |
Output is correct |
90 |
Correct |
290 ms |
85348 KB |
Output is correct |
91 |
Correct |
289 ms |
85384 KB |
Output is correct |
92 |
Correct |
304 ms |
85380 KB |
Output is correct |