Brain-hole problems. Open the number group to record the left and right portions of each point.
As long as the first point and the last point are known, the situation of the remaining points can be deduced from the efficiency of O (n).
#include <stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespacestd;Const intmaxn=10000;Long LongL[MAXN],R[MAXN],A[MAXN];intMain () {intN,i; while(~SCANF ("%d",&N)) {memset (L,0,sizeof(L)); memset (R,0,sizeof(R)); for(i=0; i<n; i++) scanf ("%lld",&A[i]); Sort (A,a+N); for(I=n; i>=1; i--) a[i]=a[i-1]; l[1]=0; for(i=2; i<=n; i++) r[1]=r[1]+a[i]-a[1]; for(i=1; i<=n-1; i++) l[n]=l[n]+a[n]-A[i]; R[n]=0; for(i=2; i<=n-1; i++) L[i]=l[i-1]+ (I-1) * (a[i]-a[i-1]); for(i=n-1; i>=2; i--) R[i]=r[i+1]+ (n-i) * (a[i+1]-A[i]); Long Longsumm=0; for(i=1; i<=n; i++) Summ=summ+l[i]+R[i]; printf ("%lld\n", Summ); } return 0;}
POJ2231 Moo Volume