参考:http://blog.csdn.net/kamiyoru/article/details/78791923
当读入第k个数字的时候:
ans′=∑k−1i=1(numberk−nubmeri)=(k−1)numberk−∑k−1i=1nubmeri
当然这不是实际的答案,因为差值为1及以内的数是不必加到答案上的;
所以
ans=ans′−countnumberk−1+countnumberk+1
include <cstdio>
#include <map>
using namespace std;
map<int,int> cnt;//计数器
int main(){
int n;long double ans=0,sum=0,num;scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%Lf",&num);
ans+=num*i-sum+cnt[(int)num+1]-cnt[(int)num-1];
sum+=num;
cnt[(int)num]++;
}
printf("%.0Lf",ans+0.000000001);
}