#include<bits/stdc++.h>
#define MAXN 1000007
using namespace std;
int n,s,p[MAXN],fr,last,mins,maxs,pref[MAXN],curr;
bool li[MAXN];
long long ans;
int k,cycle[MAXN];
pair<int,int> where[MAXN];
void dfs(int x){
li[x]=true;
cycle[x]=k;
ans+=abs(x-p[x]);
mins=min(mins,x);
maxs=max(maxs,x);
if(!li[p[x]])dfs(p[x]);
}
long long minimum_walk(vector<int> P, int S){
n=int(P.size()); s=S+1;
for(int i=1;i<=n;i++){
p[i]=P[i-1]+1;
li[i]=false; pref[i]=0;
}
last=0; fr=n+1; k=0; ans=0;
for(int i=1;i<=n;i++){
if(!li[i]){
if(p[i]!=i)last=i;
dfs(i);
}
}
for(int i=1;i<=n;i++)li[i]=false;
for(int i=n;i>=1;i--){
if(!li[i]){
k++;
if(p[i]!=i)fr=i;
mins=n; maxs=1;
dfs(i);
where[k]={mins,maxs};
pref[mins]++; pref[maxs]--;
}
}
ans/=2;
for(int i=1;i<=n-1;i++){
curr+=pref[i];
if(i<s)continue;
if(curr==0 and last>i)ans+=2;
}
curr=0;
for(int i=n;i>=2;i--){
curr+=pref[i];
if(i>s)continue;
if(curr==0 and fr<i)ans+=2;
}
/// more steps inside initial cycle
vector<int> l={0},r={0};
int from=s,to=s,ll=s,rr=s;
while(true){
while(ll>=from){
if(where[cycle[ll]].second>=s and l.back()>0){
l.push_back(0);
}
from=min(from,where[cycle[ll]].first);
to=max(to,where[cycle[ll]].second);
ll--;
}
while(rr<=to){
if(where[cycle[rr]].first<=s and r.back()>0){
r.push_back(0);
}
from=min(from,where[cycle[rr]].first);
to=max(to,where[cycle[rr]].second);
rr++;
}
if(ll<from and rr>to){
if(from==1 or to==n)break;
if(where[cycle[ll]].second>=s and where[cycle[rr]].first<=s){
l[l.size()-1]++; r[r.size()-1]++;
l.push_back(0); r.push_back(0);
from--; to++;
}else if(where[cycle[ll]].second>=s){
r[r.size()-1]++; to++;
}else{
l[l.size()-1]++; from--;
}
}
}
for(int i=0;i<l.size();i++){
ans+=2*min(l[i],r[i]);
}
return ans;
}
/*int main(){
cout<<minimum_walk({12,1,9,3,7,5,6,4,8,2,11,10,0},6)<<"\n";
vector<int> perm;
for(int i=0;i<20;i++){
perm.push_back(i);
}
while(true){
random_shuffle(perm.begin(),perm.end());
for(int i=0;i<20;i++){
cout<<perm[i]<<" ";
}
cout<<"\n";
int z=rand()%20;
cout<<z<<"\n";
minimum_walk(perm,z);
}
return 0;
}*/
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:111:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int i=0;i<l.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
1 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
1 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
1 ms |
8640 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
2 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8528 KB |
Output is correct |
17 |
Correct |
1 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
1 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
1 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
1 ms |
8640 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
2 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8528 KB |
Output is correct |
17 |
Correct |
1 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
19 |
Correct |
2 ms |
8528 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
1 ms |
8528 KB |
Output is correct |
22 |
Correct |
2 ms |
8528 KB |
Output is correct |
23 |
Correct |
1 ms |
8528 KB |
Output is correct |
24 |
Correct |
2 ms |
8528 KB |
Output is correct |
25 |
Correct |
2 ms |
8528 KB |
Output is correct |
26 |
Correct |
2 ms |
8700 KB |
Output is correct |
27 |
Correct |
2 ms |
8764 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
1 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
1 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
1 ms |
8640 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
2 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8528 KB |
Output is correct |
17 |
Correct |
1 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
19 |
Correct |
2 ms |
8528 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
1 ms |
8528 KB |
Output is correct |
22 |
Correct |
2 ms |
8528 KB |
Output is correct |
23 |
Correct |
1 ms |
8528 KB |
Output is correct |
24 |
Correct |
2 ms |
8528 KB |
Output is correct |
25 |
Correct |
2 ms |
8528 KB |
Output is correct |
26 |
Correct |
2 ms |
8700 KB |
Output is correct |
27 |
Correct |
2 ms |
8764 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8528 KB |
Output is correct |
30 |
Correct |
122 ms |
22344 KB |
Output is correct |
31 |
Correct |
120 ms |
22272 KB |
Output is correct |
32 |
Correct |
76 ms |
28672 KB |
Output is correct |
33 |
Correct |
84 ms |
28672 KB |
Output is correct |
34 |
Correct |
84 ms |
28672 KB |
Output is correct |
35 |
Correct |
92 ms |
28672 KB |
Output is correct |
36 |
Correct |
85 ms |
24572 KB |
Output is correct |
37 |
Correct |
77 ms |
24648 KB |
Output is correct |
38 |
Correct |
77 ms |
22344 KB |
Output is correct |
39 |
Correct |
78 ms |
22268 KB |
Output is correct |
40 |
Correct |
82 ms |
22268 KB |
Output is correct |
41 |
Correct |
100 ms |
22348 KB |
Output is correct |
42 |
Correct |
94 ms |
22272 KB |
Output is correct |
43 |
Correct |
85 ms |
28744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
1 ms |
8528 KB |
Output is correct |
3 |
Incorrect |
1 ms |
8528 KB |
3rd lines differ - on the 1st token, expected: '5920', found: '5614' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
1 ms |
8528 KB |
Output is correct |
4 |
Correct |
1 ms |
8528 KB |
Output is correct |
5 |
Correct |
2 ms |
8528 KB |
Output is correct |
6 |
Correct |
2 ms |
8528 KB |
Output is correct |
7 |
Correct |
1 ms |
8528 KB |
Output is correct |
8 |
Correct |
2 ms |
8528 KB |
Output is correct |
9 |
Correct |
2 ms |
8528 KB |
Output is correct |
10 |
Correct |
2 ms |
8528 KB |
Output is correct |
11 |
Correct |
1 ms |
8528 KB |
Output is correct |
12 |
Correct |
2 ms |
8528 KB |
Output is correct |
13 |
Correct |
1 ms |
8640 KB |
Output is correct |
14 |
Correct |
2 ms |
8528 KB |
Output is correct |
15 |
Correct |
2 ms |
8528 KB |
Output is correct |
16 |
Correct |
2 ms |
8528 KB |
Output is correct |
17 |
Correct |
1 ms |
8528 KB |
Output is correct |
18 |
Correct |
2 ms |
8528 KB |
Output is correct |
19 |
Correct |
2 ms |
8528 KB |
Output is correct |
20 |
Correct |
2 ms |
8528 KB |
Output is correct |
21 |
Correct |
1 ms |
8528 KB |
Output is correct |
22 |
Correct |
2 ms |
8528 KB |
Output is correct |
23 |
Correct |
1 ms |
8528 KB |
Output is correct |
24 |
Correct |
2 ms |
8528 KB |
Output is correct |
25 |
Correct |
2 ms |
8528 KB |
Output is correct |
26 |
Correct |
2 ms |
8700 KB |
Output is correct |
27 |
Correct |
2 ms |
8764 KB |
Output is correct |
28 |
Correct |
2 ms |
8696 KB |
Output is correct |
29 |
Correct |
2 ms |
8528 KB |
Output is correct |
30 |
Correct |
122 ms |
22344 KB |
Output is correct |
31 |
Correct |
120 ms |
22272 KB |
Output is correct |
32 |
Correct |
76 ms |
28672 KB |
Output is correct |
33 |
Correct |
84 ms |
28672 KB |
Output is correct |
34 |
Correct |
84 ms |
28672 KB |
Output is correct |
35 |
Correct |
92 ms |
28672 KB |
Output is correct |
36 |
Correct |
85 ms |
24572 KB |
Output is correct |
37 |
Correct |
77 ms |
24648 KB |
Output is correct |
38 |
Correct |
77 ms |
22344 KB |
Output is correct |
39 |
Correct |
78 ms |
22268 KB |
Output is correct |
40 |
Correct |
82 ms |
22268 KB |
Output is correct |
41 |
Correct |
100 ms |
22348 KB |
Output is correct |
42 |
Correct |
94 ms |
22272 KB |
Output is correct |
43 |
Correct |
85 ms |
28744 KB |
Output is correct |
44 |
Correct |
2 ms |
8528 KB |
Output is correct |
45 |
Correct |
1 ms |
8528 KB |
Output is correct |
46 |
Incorrect |
1 ms |
8528 KB |
3rd lines differ - on the 1st token, expected: '5920', found: '5614' |
47 |
Halted |
0 ms |
0 KB |
- |