#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;
t=max(t,mx);
}
mx=st[1];
for(i=1;i<=t;i++)
{
if(mx<st[i])nr++;
mx=max(mx,st[i]);
}
return ans+2LL*nr;
}
/*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 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
384 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 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Incorrect |
2 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '4', found: '6' |
6 |
Halted |
0 ms |
0 KB |
- |