이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
int n;
vector<int> arr;
int fwd[1000005];
vector<int> imp;
long long minimum_walk(std::vector<int> p, int s) {
n=sz(p);
arr=p;
rep(x,0,n){
if (x<arr[x]){
fwd[x]++,fwd[arr[x]]--;
imp.push_back(x);
imp.push_back(arr[x]);
}
else if (arr[x]<x){
imp.push_back(x);
imp.push_back(arr[x]);
}
}
if (imp.empty()) return 0;
rep(x,0,n) fwd[x+1]+=fwd[x];
int l=0,r=n-1;
while (fwd[l]==0) l++;
while (fwd[r]==0) r--;
ll ans=1e9;
for (auto &it:imp) ans=min(ans,(ll)abs(s-it));
rep(x,l,r+1){
ans+=max(fwd[x],1);
}
return ans*2;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |