• Moo Volume POJ


    Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise. 

    FJ's N cows (1 <= N <= 10,000) all graze at various locations on a long one-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.

    Input

    * Line 1: N 

    * Lines 2..N+1: The location of each cow (in the range 0..1,000,000,000).

    Output

    There are five cows at locations 1, 5, 3, 2, and 4.

    Sample Input

    5
    1
    5
    3
    2
    4

    Sample Output

    40

    Hint

    INPUT DETAILS: 

    There are five cows at locations 1, 5, 3, 2, and 4. 

    OUTPUT DETAILS: 

    Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at 4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.
     
     
    求任意两个点之间的距离总和;
    这题数据大肯定不能直接求,要找规律;
    总共有(n-i-1)*(a[n-i-1]-a[i])算出了每两个点的距离 
    这总共是一半的距离和 所以要乘以2
     
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cctype>
     7 using namespace std;
     8 long long a[10010];
     9 int main() {
    10     int n;
    11     while(scanf("%d",&n)!=EOF){
    12         for (int i=0 ;i<n ;i++)
    13             scanf("%lld",&a[i]);
    14         sort(a,a+n);
    15         long long sum=0;
    16         for (int i=0 ;i<n ;i++ )
    17             sum+=(n-1-i)*(a[n-1-i]-a[i]);
    18         printf("%lld
    ",sum*2);
    19     }
    20     return 0;
    21 }
  • 相关阅读:
    leetcode_1423. 可获得的最大点数
    leetcode_剑指 Offer 06. 从尾到头打印链表
    leetcode_剑指 Offer 05. 替换空格
    leetcode_49. 字母异位词分组
    leetcode_73. 矩阵置零
    leetcode_26. 删除排序数组中的重复项
    jstack查看JVM堆栈信息
    如何画一张架构图
    百年孤独家谱
    阿尔萨斯(Arthas)入门
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8522464.html
Copyright © 2020-2023  润新知