题目链接:http://codeforces.com/problemset/problem/612/B
解题思路:
一开始看错了题意,他要求的是从1-n所耗费的时间,n表示的是数值而不是下标,
实现代码:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <bitset> using namespace std; #define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++) #define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--) #define pb push_back #define mp make_pair const int inf_int = 2e9; const long long inf_ll = 2e18; #define inf_add 0x3f3f3f3f #define mod 1000000007 #define LL long long #define ULL unsigned long long #define MS0(X) memset((X), 0, sizeof((X))) #define SelfType int SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);} SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;} #define Sd(X) int (X); scanf("%d", &X) #define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define all(a) a.begin(), a.end() #define mem(x,v) memset(x,v,sizeof(x)) typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vll; struct node{ int num,id; }a[200009]; bool cmp(const node x,const node y){ return x.num<y.num; } int main() { LL m,ans=0,i; cin>>m; for(i=1;i<=m;i++){ cin>>a[i].num; a[i].id = i; } sort(a+1,a+1+m,cmp); for(i=1;i<m;i++) ans+=abs(a[i+1].id-a[i].id); cout<<ans<<endl; }