zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    MS SqlServer学习笔记(索引)
    Angular动态注册组件(controller,service...)
    如何成功发布一个MSMQ的Windows服务
    主流Web服务器一览
    .NET 创建Windows服务,及服务的安装卸载
    SQL Server 查询时间段内数据
    委托和事件
    类中实现 Dispose And Finalize
    使用 ODBC .NET 提供程序和 Visual C# .NET 执行 SQL 参数化存储过程
    Windows Form 中快捷键设置
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8522464.html
Copyright © 2011-2022 走看看