#include "books.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first.first
#define S first.second
#define T second
long long minimum_walk(vector<int> p, int s){
ll n=p.size(), r=0, x=n, y=0, z=-1;
bool b[n];
for(int i=0; i<n; i++){
b[i]=0;
}
vector<pair<pair<ll, ll>, vector<ll>>> v;
vector<ll> v1;
for(ll i=0; i<n; i++){
if(!b[i]){
v1.clear();
//cout << z << " ";
if(i>=z){
if(z!=-1)r+=2*(i-z);
}
r+=p[i]-i;
y=p[i];
while(y!=i){
v1.push_back(y);
z=max(z, y);
b[y]=1;
r+=abs(p[y]-y);
y=p[y];
}
v1.push_back(y);
b[y]=1;
z=max(z, y);
v.push_back({{i, z}, v1});
}
}
if(v.empty())return 0;
unique(v.begin(), v.end());
sort(v.begin(), v.end());
if(s>v.back().S)return r+2*(s-v.back().S);
if(s<v[0].F) return r+2*(v[0].F-s);
x=n;
// z=0;
// for(int i=0; i<v.size(); i++){
// if(i+1==v.size()){
// if(s>=v[i].F&&s<=v[i].S&&v[i].S>z){
// //cout << v[i].F << " " << v[i].S << "\n";
// for(int t=0; t<v[i].T.size(); t++)x=min(x, abs(s-v[i].T[t]));
// z=v[i].S;
// }
// }
// else if(v[i+1].F!=v[i].F){
// if(s>=v[i].F&&s<=v[i].S&&v[i].S>z){
// for(int t=0; t<v[i].T.size(); t++)x=min(x, abs(s-v[i].T[t]));
// z=v[i].S;
// }
// }
// }
if(x==n)return 0;
return r+2*x;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '6', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '6', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '6', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
3rd lines differ - on the 1st token, expected: '6', found: '0' |
2 |
Halted |
0 ms |
0 KB |
- |