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.

 
贴两种方法:
第一种:
就是找规律,这个特别难想。
先对a数组进行排序,然后求出相邻的差分
之后就根据n头牛,和第i头牛直接差分用的次数找到规律,直接计算。
 
第二种,比较简单
就是也要排序
然后第i头牛的音量就是第i-1头牛再加上(i-1-1)*d  (d是i和i-1的距离) 再减去 (n-i)*d
其实每头之间牛音量的不同就在于他们的距离,所以对他们的距离进行处理即可。
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;//要找规律!!!
ll a[11000];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
sort(a,a+n);
ll sum=0;
for(int i=1;i<n;i++)
{
sum+=(a[i]-a[i-1])*i*(n-i)*2;
}
cout<<sum<<endl;
return 0;
}

  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll a[11000],b[11000];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    sort(a+1,a+n+1);
    for(int i=2;i<=n;i++)
    {
        b[1]+=abs(a[i]-a[1]);
    }
    ll sum=b[1];
    for(int i=2;i<=n;i++)
    {
        ll d=a[i]-a[i-1];
        b[i]=b[i-1]+(i-1-1)*d-(n-i)*d;
        sum+=b[i];
    }
    cout<<sum<<endl;
    return 0;
}

  

 
 

B - Moo Volume的更多相关文章

  1. BZOJ1679: [Usaco2005 Jan]Moo Volume 牛的呼声

    1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 723  Solved: 346[ ...

  2. BZOJ 1679: [Usaco2005 Jan]Moo Volume 牛的呼声( )

    一开始直接 O( n² ) 暴力..结果就 A 了... USACO 数据是有多弱 = = 先sort , 然后自己再YY一下就能想出来...具体看code --------------------- ...

  3. Poj2231 Moo Volume 2017-03-11 22:58 30人阅读 评论(0) 收藏

    Moo Volume Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22104   Accepted: 6692 Descr ...

  4. POJ 2231 Moo Volume

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Farmer Jo ...

  5. Moo Volume POJ - 2231

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

  6. 【BZOJ】1679: [Usaco2005 Jan]Moo Volume 牛的呼声(数学)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1679 水题没啥好说的..自己用笔画画就懂了 将点排序,然后每一次的点到后边点的声音距离和==(n-i ...

  7. Poj 2232 Moo Volume(排序)

    题目链接:http://poj.org/problem?id=2231 思路分析:先排序,再推导计算公式. 代码如下: #include <iostream> #include <a ...

  8. bzoj 1679: [Usaco2005 Jan]Moo Volume 牛的呼声【枚举】

    直接枚举两两牛之间的距离即可 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  9. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

随机推荐

  1. [android] android下junit测试框架配置

    我们的业务代码一般是放在一个新的包下面,这个业务类不能够通过右键run as java application,因为android项目只能运行在手机上的dalvak虚拟机里面 新建一个包,里面写测试类 ...

  2. 32.QT-制作最强电压电阻表盘,可以自定义阴影效果,渐变颜色,图标,文字标签等-附带demo程序

    由于上位机需要绘制电压电阻表盘,如下图所示: 后来,在网上找阿找,还是没找到满意的,索性自己来画控件算了,由于第一次画控件,所以花了我2天时间,才画好 效果图如下: 上图的所有颜色(包括滑动的渐变/单 ...

  3. Java高并发--消息队列

    Java高并发--消息队列 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 举个例子:在购物商城下单后,希望购买者能收到短信或者邮件通知.有一种做法时在下单逻辑执行后调 ...

  4. Hibernate入门(十)inverse

    双向关联产生多余的SQL语句 /** * 添加一个订单到id为6的客户中 */ @Test public void fun1(){ Session session = HibernateUtils.g ...

  5. 7.通用程序设计_EJ

    第45条: 将局部变量的作用域最小化 该条目与第13条(使类和成员的可访问性最小)本质上是类似的.要使局部变量的作用域最小化,最有利的方法就是在第一次使用它的地方声明.在每个局部变量的声明处都应该包含 ...

  6. javascript 点击触发复制功能

    摘要: js调用复制功能使用: document.execCommand("copy", false); document.execCommand()方法功能很强大,了解更多请戳: ...

  7. nodeJs express mongodb 建站(window 10 版)

    一.环境搭建 安装 node.git.npm.express.mongodb.主要介绍express.mongodb 的安装. (1)node安装:https://nodejs.org/en/down ...

  8. blfs(systemd版本)学习笔记-为桌面环境构建xorg服务

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs准备使用桌面环境,首先需要构建xorg服务 xorg服务项目地址:http://www.linuxfromscratch. ...

  9. Nginx 参数配置相关

    Nginx参数配置相关 by:授客 QQ:1033553122 目的: 对Nginx配置的点滴学习总结,主要目的在于分析Nginx与性能相关的一些参数设置,以便性能调优时选择最优配置   环境: $ ...

  10. Android星球效果实现

    在项目中看着这个旋转效果挺炫的,就抽取出来做个记录.主要是使用CarrouselLayout 稍微修改 CarrouselLayout代码Demo下载z地址:GitHub https://github ...