이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
long long minimum_walk(vector<int> p, int s){
int n = (int)p.size();
ll ans = 0;
vector<bool> vis(n);
int mi = s, ma = s;
int l = s, r = s;
while(true){
int i = -1;
while(r <= ma){
if(!vis[r]){
i = r;
break;
}
r++;
}
if(i != -1)
while(l >= mi){
if(!vis[l]){
i = l;
break;
}
l--;
}
if(i == -1){
int oldR = r, oldL = l;
while(r < n){
if(p[r] != r){
i = r;
ans += 2*(r+1-oldR);
break;
}
r++;
}
if(i == -1)
while(l >= 0){
if(p[l] != l){
i = l;
ans += 2*(oldL+1-l);
break;
}
l--;
}
if(i == -1) break;
}
int j = i;
do
{
vis[j] = true;
mi = min(mi, p[j]);
ma = max(ma, p[j]);
ans += abs(j-p[j]);
j = p[j];
}while(j != i);
}
return ans;
}
# | 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... |