#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){
from=min(from,where[cycle[ll]].first);
to=max(to,where[cycle[ll]].second);
ll--;
}
while(rr<=to){
from=min(from,where[cycle[rr]].first);
to=max(to,where[cycle[rr]].second);
rr++;
}
if(ll<from and rr>to){
if(from==1 and 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:103:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i=0;i<l.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
17232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
17232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
17232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Runtime error |
20 ms |
17236 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
18 ms |
17232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |