#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e6;
const ll inf = 1e18;
ll ps[N + 1];
long long minimum_walk(std::vector<int> p, int s) {
int n = p.size();
ll ans = 0;
ll mx = -inf;
ll mn = inf;
ll mx2 = -inf;
ll mn2 = inf;
bool ok = 0;
for(ll i = 0;i < n;i++){
ans+=abs(i - p[i]);
if(i != p[i]){
ok = 1;
if(i < p[i]){
ps[p[i]]--;
ps[i]++;
}
if(i <= s){
mx = max(mx,i);
}
if(i >= s){
mn = min(mn,i);
}
mx2 = max(mx2,i);
mn2 = min(mn2,i);
}
}
if(ok == 0)return ans;
ll sum = 0;
for(int i = mn2;i < mx2;i++){
sum+=ps[i];
//cout<<ps[i]<<' ';
if(sum == 0)ans+=2;
}
//cout<<ans<<' '<<mx - s<<' '<<s - mn<<'\n';
if(mx == -inf)mx = inf;
if(mn == inf)mn = -inf;
ans+=2*min(mx - s,s - mn);
return ans;
}
/**
4 0
3 2 1 0
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 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 |
348 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 |
348 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 |
348 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 |
348 KB |
3rd lines differ - on the 1st token, expected: '6', found: '2' |
2 |
Halted |
0 ms |
0 KB |
- |