主要是存储数组下标,可以用结构体,也可建立两个数组,一个用于存储下标,形如a[b[x]]即可
#include<stdio.h>
#include<algorithm>
using namespace std;
struct note {
int x;
int id;
} s[10000100];
bool judge(note a,note b) {
return a.x<b.x;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF) {
__int64 sum=0;
for(int l=0; l<n; l++) {
scanf("%d",&s[l].x);
s[l].id=l;
}
sort(s,s+n,judge);
for(int l=0; l<n-1; l++)
sum+=abs(s[l+1].id-s[l].id);
printf("%I64d\n",sum);
}
return 0;
}