发现每条线段的贡献是长度*左边点的个数*右边点的个数。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 10050 using namespace std; long long n,a[maxn]; int main() { scanf("%lld",&n); for (long long i=1;i<=n;i++) scanf("%lld",&a[i]); sort(a+1,a+n+1); long long ans=0; for (long long i=2;i<=n;i++) ans+=(a[i]-a[i-1])*(i-1)*(n-i+1); printf("%lld\n",ans*2); return 0; }
时间: 2024-11-15 00:26:40