【CodeForces】[612B]HDD is Outdated Technology
记录数字的下标存入a[t]=i
然后直接累加abs(a[i+1]-a[i])
#include<stdio.h>
int a[1000000];
int abs(int m){
return m>0?m:-m;
}
int main() {
int n;
while(scanf("%d",&n)!=EOF) {
for(int i=1; i<=n; i++) {
int t;
scanf("%d",&t);
a[t]=i;
}
__int64 sum=0;
for(int i=1; i<n; i++) {
sum+=(__int64)abs(a[i]-a[i+1]);
}
printf("%I64d\n",sum);
}
return 0;
}