Next Spaceship

Driving into future…

POJ 2231 Moo Volume

| Comments

Here is my pseudo code

1
2
3
4
5
Sort the cows by their positions.
For each adjencent cows c(i) and c[i-1] (i is from 2 to n)do
    calculate the distance d(i) between them
    multiply d(i) with (i-1)*(n-i+1) is the contribution of this distance v(i)
The sum of all v(i) times 2 is the result.

Source code:

POJ 2231 Moo Volume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
using namespace std;
int a[10010],b[10010],n;
__int64 r;
int main(){
    int i;
    cin>>n;
    for(i=1;i<=n/2;i++){
        b[i]=(n-i)*i;
        b[n-i]=(n-i)*i;
    }
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    r=0;
    for(i=1;i<n;i++){
        r+=(__int64)(a[i]-a[i-1])*b[i];
    }
    printf("%I64d",r*2);
    return 0;
}

Comments