#pragma GCC optimize("O3")
#include "books.h"
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e6+50;
const int mod=1e9+7;
using namespace std;
int n,s,p[nmax],mn,mx,t,i,st[nmax],v[nmax],j;
ll ans,nr;
ll minimum_walk(vector<int>x,int s)
{
n=(int)x.size();
for(i=1;i<=n;i++)p[i]=x[i-1]+1;
for(i=1;i<=n;i++)ans+=1LL*abs(i-p[i]);
for(i=1;i<=n;i++)
{
if(v[p[i]])continue;
j=p[i];
mn=1e9,mx=-1;
while(!v[j])
{
v[j]=1;
mn=min(mn,j);
mx=max(mx,j);
j=p[j];
}
st[mn]=mx;
if(mn!=mx)t=max(t,mx);
}
for(i=1;i<=t;i++)
{
mx=max(mx,st[i]);
if(i==mx)ans+=2LL;
}
if(!t)return 0LL;
return ans-2LL;
}
/*int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>s;
for(i=1;i<=n;i++)cin>>p[i];
for(i=1;i<=n;i++)ans+=abs(i-p[i]);
for(i=1;i<=n;i++)
{
if(v[p[i]])continue;
j=p[i];
mn=1e9,mx=-1;
while(!v[j])
{
v[j]=1;
mn=min(mn,j);
mx=max(mx,j);
j=p[j];
}
st[mn]=mx;
t=max(t,mx);
}
mx=st[1];
for(i=1;i<=t;i++)
{
if(mx<st[i])nr++;
mx=max(mx,st[i]);
}
cout<<ans+2*nr<<endl;
return 0;
}
/*
4 1
1 3 4 2
*/
Compilation message
books.cpp:81:1: warning: "/*" within comment [-Wcomment]
/*
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
356 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '2744' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |