POJ2231 Moo Volume

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.