#include <bits/stdc++.h>
using namespace std;
using ll =long long;
template<class t,class u>bool chmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool chmin(t&a,u b){if(b<a)a=b;return b<a;}
#define N 1000005
int l[N],r[N];
bool vis[N];
void extend(int &nl,int &nr,int tl=-1,int tr=-1){
if(tl==-1)tl=nl+1;
if(tr==-1)tr=nl;
while(nl<tl||tr<nr){
if(nl<tl){
tl--;
chmin(nl,l[tl]);
chmax(nr,r[tl]);
}
if(tr<nr){
tr++;
chmin(nl,l[tr]);
chmax(nr,r[tr]);
}
}
}
ll minimum_walk(vector<int>p, int s){
int n=p.size();
for(int i=0;i<n;i++){
int v=i;
while(!vis[v]){
vis[v]=true;
l[v]=i;
v=p[v];
}
}
for(int i=0;i<n;i++)vis[i]=false;
for(int i=n-1;i>=0;i--){
int v=i;
while(!vis[v]){
vis[v]=true;
r[v]=i;
v=p[v];
}
}
int gl=0,gr=n-1;
while(gl<n&&p[gl]==gl)gl++;
while(gr>=0&&p[gr]==gr)gr--;
if(gl>gr)return 0;
ll ans=0;
for(int i=0;i<n;i++)ans+=abs(p[i]-i);
int nl=s,nr=s;
extend(nl,nr);
while(gl<nl||nr<gr){
ll tmpl=0;
int l_nxtl=nl,l_nxtr=nr;
while(gl<l_nxtl&&l_nxtr<=nr){
tmpl+=2;
l_nxtl--;
extend(l_nxtl,l_nxtr,l_nxtl+1,l_nxtr);
}
ll tmpr=0;
int r_nxtl=nl,r_nxtr=nr;
while(gr>r_nxtr&&r_nxtl>=nl){
tmpr+=2;
r_nxtr++;
extend(r_nxtl,r_nxtr,r_nxtl,r_nxtr-1);
}
if(l_nxtl==r_nxtl&&l_nxtr==r_nxtr){
ans+=min(tmpl,tmpr);
nl=l_nxtl;
nr=l_nxtr;
}else{
ans+=tmpl;
ans+=tmpr;
nl=min(l_nxtl,r_nxtl);
nr=max(l_nxtr,r_nxtr);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 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 |
4696 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 |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 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 |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 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 |
4696 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 |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 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 |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4696 KB |
Output is correct |
20 |
Correct |
1 ms |
4548 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4456 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4556 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 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 |
4696 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 |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 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 |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4696 KB |
Output is correct |
20 |
Correct |
1 ms |
4548 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4456 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4556 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
101 ms |
16888 KB |
Output is correct |
31 |
Correct |
102 ms |
16976 KB |
Output is correct |
32 |
Correct |
74 ms |
16976 KB |
Output is correct |
33 |
Correct |
91 ms |
16896 KB |
Output is correct |
34 |
Correct |
84 ms |
17064 KB |
Output is correct |
35 |
Correct |
99 ms |
16892 KB |
Output is correct |
36 |
Correct |
84 ms |
16888 KB |
Output is correct |
37 |
Correct |
77 ms |
16904 KB |
Output is correct |
38 |
Correct |
77 ms |
16892 KB |
Output is correct |
39 |
Correct |
76 ms |
16912 KB |
Output is correct |
40 |
Correct |
80 ms |
16976 KB |
Output is correct |
41 |
Correct |
99 ms |
16896 KB |
Output is correct |
42 |
Correct |
98 ms |
17148 KB |
Output is correct |
43 |
Correct |
81 ms |
16908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 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 |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4544 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 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 |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 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 |
4696 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 |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4696 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 |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4696 KB |
Output is correct |
20 |
Correct |
1 ms |
4548 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4456 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4556 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
101 ms |
16888 KB |
Output is correct |
31 |
Correct |
102 ms |
16976 KB |
Output is correct |
32 |
Correct |
74 ms |
16976 KB |
Output is correct |
33 |
Correct |
91 ms |
16896 KB |
Output is correct |
34 |
Correct |
84 ms |
17064 KB |
Output is correct |
35 |
Correct |
99 ms |
16892 KB |
Output is correct |
36 |
Correct |
84 ms |
16888 KB |
Output is correct |
37 |
Correct |
77 ms |
16904 KB |
Output is correct |
38 |
Correct |
77 ms |
16892 KB |
Output is correct |
39 |
Correct |
76 ms |
16912 KB |
Output is correct |
40 |
Correct |
80 ms |
16976 KB |
Output is correct |
41 |
Correct |
99 ms |
16896 KB |
Output is correct |
42 |
Correct |
98 ms |
17148 KB |
Output is correct |
43 |
Correct |
81 ms |
16908 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
45 |
Correct |
1 ms |
4444 KB |
Output is correct |
46 |
Correct |
1 ms |
4440 KB |
Output is correct |
47 |
Correct |
1 ms |
4444 KB |
Output is correct |
48 |
Correct |
1 ms |
4444 KB |
Output is correct |
49 |
Correct |
1 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 |
1 ms |
4444 KB |
Output is correct |
53 |
Correct |
1 ms |
4544 KB |
Output is correct |
54 |
Correct |
1 ms |
4444 KB |
Output is correct |
55 |
Correct |
1 ms |
4444 KB |
Output is correct |
56 |
Correct |
1 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 |
4440 KB |
Output is correct |
63 |
Correct |
1 ms |
4444 KB |
Output is correct |
64 |
Correct |
83 ms |
23664 KB |
Output is correct |
65 |
Correct |
83 ms |
23632 KB |
Output is correct |
66 |
Correct |
80 ms |
23640 KB |
Output is correct |
67 |
Correct |
80 ms |
23796 KB |
Output is correct |
68 |
Correct |
9 ms |
5980 KB |
Output is correct |
69 |
Correct |
9 ms |
5976 KB |
Output is correct |
70 |
Correct |
10 ms |
5976 KB |
Output is correct |
71 |
Correct |
10 ms |
5980 KB |
Output is correct |
72 |
Correct |
9 ms |
5976 KB |
Output is correct |
73 |
Correct |
9 ms |
5976 KB |
Output is correct |
74 |
Correct |
87 ms |
23548 KB |
Output is correct |
75 |
Correct |
96 ms |
23672 KB |
Output is correct |
76 |
Correct |
86 ms |
23716 KB |
Output is correct |
77 |
Correct |
91 ms |
23664 KB |
Output is correct |
78 |
Correct |
87 ms |
23584 KB |
Output is correct |
79 |
Correct |
85 ms |
23588 KB |
Output is correct |
80 |
Correct |
72 ms |
23804 KB |
Output is correct |
81 |
Correct |
79 ms |
23800 KB |
Output is correct |
82 |
Correct |
78 ms |
23632 KB |
Output is correct |
83 |
Correct |
89 ms |
23804 KB |
Output is correct |
84 |
Correct |
89 ms |
23804 KB |
Output is correct |
85 |
Correct |
86 ms |
23636 KB |
Output is correct |
86 |
Correct |
87 ms |
23668 KB |
Output is correct |
87 |
Correct |
87 ms |
23632 KB |
Output is correct |
88 |
Correct |
96 ms |
23676 KB |
Output is correct |
89 |
Correct |
90 ms |
23680 KB |
Output is correct |
90 |
Correct |
82 ms |
23632 KB |
Output is correct |
91 |
Correct |
91 ms |
23548 KB |
Output is correct |
92 |
Correct |
89 ms |
23676 KB |
Output is correct |