把n个点依次标为1..n;
然后按照数字大小给每个人排序(记录它原来的位置);
走到第一个人,然后依次走到下一个人即可;
每次枚举往左走还是往右走即可;
0
/*
*/
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct node{
int num,pos;
};
node a[(int) 1e5+10];
int n;
bool cmp(node a,node b){
return a.num < b.num;
}
main(){
scanf("%lld",&n);
for (int i = 1;i <= n;i++){
int x;
scanf("%lld",&x);
a[i].num = x,a[i].pos = i;
}
sort(a+1,a+1+n,cmp);
int x = a[1].pos,ans = 0;
for (int i = 2;i <= n;i++){
int temp;
if (a[i].pos<=x){
temp = min(x-a[i].pos,n-x+a[i].pos);
}else
temp = min(a[i].pos-x,x+n-a[i].pos);
ans+=temp;
x = a[i].pos;
}
printf("%lld
",ans);
return 0;
}