#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+=max(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 |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 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 |
4444 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 |
4448 KB |
Output is correct |
17 |
Correct |
1 ms |
4448 KB |
Output is correct |
18 |
Correct |
0 ms |
4548 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 |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 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 |
4444 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 |
4448 KB |
Output is correct |
17 |
Correct |
1 ms |
4448 KB |
Output is correct |
18 |
Correct |
0 ms |
4548 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
21 |
Correct |
1 ms |
4448 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4448 KB |
Output is correct |
24 |
Correct |
1 ms |
4448 KB |
Output is correct |
25 |
Correct |
1 ms |
4448 KB |
Output is correct |
26 |
Correct |
1 ms |
4448 KB |
Output is correct |
27 |
Correct |
1 ms |
4452 KB |
Output is correct |
28 |
Correct |
1 ms |
4452 KB |
Output is correct |
29 |
Correct |
1 ms |
4452 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 |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 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 |
4444 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 |
4448 KB |
Output is correct |
17 |
Correct |
1 ms |
4448 KB |
Output is correct |
18 |
Correct |
0 ms |
4548 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
21 |
Correct |
1 ms |
4448 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4448 KB |
Output is correct |
24 |
Correct |
1 ms |
4448 KB |
Output is correct |
25 |
Correct |
1 ms |
4448 KB |
Output is correct |
26 |
Correct |
1 ms |
4448 KB |
Output is correct |
27 |
Correct |
1 ms |
4452 KB |
Output is correct |
28 |
Correct |
1 ms |
4452 KB |
Output is correct |
29 |
Correct |
1 ms |
4452 KB |
Output is correct |
30 |
Correct |
120 ms |
22408 KB |
Output is correct |
31 |
Correct |
118 ms |
22216 KB |
Output is correct |
32 |
Correct |
78 ms |
22328 KB |
Output is correct |
33 |
Correct |
104 ms |
22256 KB |
Output is correct |
34 |
Correct |
108 ms |
22424 KB |
Output is correct |
35 |
Correct |
92 ms |
22360 KB |
Output is correct |
36 |
Correct |
111 ms |
22404 KB |
Output is correct |
37 |
Correct |
79 ms |
22352 KB |
Output is correct |
38 |
Correct |
105 ms |
22640 KB |
Output is correct |
39 |
Correct |
78 ms |
22356 KB |
Output is correct |
40 |
Correct |
85 ms |
22196 KB |
Output is correct |
41 |
Correct |
104 ms |
22356 KB |
Output is correct |
42 |
Correct |
113 ms |
22380 KB |
Output is correct |
43 |
Correct |
83 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3506' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4544 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
3 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 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 |
4444 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 |
4448 KB |
Output is correct |
17 |
Correct |
1 ms |
4448 KB |
Output is correct |
18 |
Correct |
0 ms |
4548 KB |
Output is correct |
19 |
Correct |
1 ms |
4448 KB |
Output is correct |
20 |
Correct |
1 ms |
4448 KB |
Output is correct |
21 |
Correct |
1 ms |
4448 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4448 KB |
Output is correct |
24 |
Correct |
1 ms |
4448 KB |
Output is correct |
25 |
Correct |
1 ms |
4448 KB |
Output is correct |
26 |
Correct |
1 ms |
4448 KB |
Output is correct |
27 |
Correct |
1 ms |
4452 KB |
Output is correct |
28 |
Correct |
1 ms |
4452 KB |
Output is correct |
29 |
Correct |
1 ms |
4452 KB |
Output is correct |
30 |
Correct |
120 ms |
22408 KB |
Output is correct |
31 |
Correct |
118 ms |
22216 KB |
Output is correct |
32 |
Correct |
78 ms |
22328 KB |
Output is correct |
33 |
Correct |
104 ms |
22256 KB |
Output is correct |
34 |
Correct |
108 ms |
22424 KB |
Output is correct |
35 |
Correct |
92 ms |
22360 KB |
Output is correct |
36 |
Correct |
111 ms |
22404 KB |
Output is correct |
37 |
Correct |
79 ms |
22352 KB |
Output is correct |
38 |
Correct |
105 ms |
22640 KB |
Output is correct |
39 |
Correct |
78 ms |
22356 KB |
Output is correct |
40 |
Correct |
85 ms |
22196 KB |
Output is correct |
41 |
Correct |
104 ms |
22356 KB |
Output is correct |
42 |
Correct |
113 ms |
22380 KB |
Output is correct |
43 |
Correct |
83 ms |
22356 KB |
Output is correct |
44 |
Incorrect |
1 ms |
4444 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3506' |
45 |
Halted |
0 ms |
0 KB |
- |