JYM的公司

【簡要題意】求序列中任意兩個數的差之和。n<=5e5

【分析】排序掃一遍,over

【code】

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=5e5+1000;
int n;LL a[maxn],s,ans;
inline void read(LL &x){
	x=0;LL fl=1;char tmp=getchar();
	while(tmp<'0'||tmp>'9'){if(tmp=='-')fl=-fl;tmp=getchar();}
	while(tmp>='0'&&tmp<='9') x=(x<<1)+(x<<3)+tmp-'0',tmp=getchar();
	x=x*fl;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) read(a[i]); sort(a+1,a+n+1);
	for(int i=1;i<=n;i++) ans=ans+(i-1)*a[i]-s,s+=a[i];
	cout<<(ans<<1)<<endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章