#include "books.h"
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define x first
#define y second
int f[1000005];
int l[1000005];
int r[1000005];
int Find(int x){
if(f[x]==x)return x;
return f[x]=Find(f[x]);
}
int pre[1000005];
long long minimum_walk(std::vector<int> p, int s) {
// vector<pii> v;
long long ans=0;
int Min=1e9;
for(int i = 0;i<p.size();i++){
if(p[i]!=i){
// v.pb(mp(p[i],i));
ans+=abs(p[i]-i);
Min=min(Min,abs(i-s));
}
l[i]=1e9;
r[i]=0;
f[i]=i;
}
for(int i = 0;i<p.size();i++){
f[Find(p[i])]=Find(i);
}
for(int i = 0;i<p.size();i++){
l[Find(i)]=min(l[Find(i)],i);
r[Find(i)]=max(r[Find(i)],i);
}
vector<pii> v;
for(int i = 0;i<p.size();i++){
if(f[i]==i&&l[i]!=r[i]){
v.pb(mp(l[i],r[i]));
}
}
if(v.empty())return 0;
sort(v.begin(),v.end());
int Max=v[0].x;
for(auto it:v){
ans+=max(0,(it.x-Max)*2);
Max=max(Max,it.y);
}
if(s>Max||s<v[0].x){
Min=min(s-Max,v[0].x-s);
}
else{
Min=0;
}
return ans+Min*2;
}
Compilation message
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:21:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i = 0;i<p.size();i++){
| ~^~~~~~~~~
books.cpp:31:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0;i<p.size();i++){
| ~^~~~~~~~~
books.cpp:34:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int i = 0;i<p.size();i++){
| ~^~~~~~~~~
books.cpp:39:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0;i<p.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '-2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '-2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '-2' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 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 |
Incorrect |
1 ms |
384 KB |
3rd lines differ - on the 1st token, expected: '6', found: '-2' |
2 |
Halted |
0 ms |
0 KB |
- |