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. VS2017 启动调试出现 无法启动程序“http://localhost:15613” 操作在当前状态中是非法的。 同时附加进程也是错误的解决方法

    第一次发表这样的博客,不会如何的排版,还有很多的不懂,大神勿喷哈! 同时是给自己做的一次记录,已方便后面可能会同样出现该问题后不用像无头苍蝇一样到处百度乱找 VS2017 启动调试出现  无法启动程序 ...

  2. Struts2_属性驱动

    在jsp页面提交到action中的表单元素在action中是以对象的形式存在的, action中的对象name必须与jsp页面中的表单元素name相同,struts框架自动为action的对象赋值. ...

  3. HDU3038(KB5-D加权并查集)

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. ASPxGridView 选中主表一行数据,从表自动选中(勾选)对应的行

    一.图解 下图为效果图,点击 [A表]种的某一行,[B表]会有与之相对于一行会被自动选中并且勾选上: 二.Html 代码 <html xmlns="http://www.w3.org/ ...

  5. CSS3的媒体查询(Media Queries)与移动设备显示尺寸大全

    媒体查询介绍 我今天就总结一下响应式设计的核心CSS技术Media(媒体查询器)的用法. 先看一个简单的例子: <link rel="stylesheet" media=&q ...

  6. [笔记]JavaScript 秘密花园

    1.hasOwnProperty相关 为了判断一个对象是否包含自定义属性而不是原型链上的属性,我们需要使用继承自 Object.prototype 的 hasOwnProperty方法.hasOwnP ...

  7. vuejs自定义过滤器根据搜索框输入的值,筛选复杂的列表数据

    如题所示,自定义过滤器根据搜索框输入的值,筛选复杂的列表数据.如图所示: html代码: <input type="text" placeholder="姓名/账号 ...

  8. CSS3布局之box-flex的使用

    语法: box-flex:<number> 其中number取值:使用浮点数指定对象所分配其父元素剩余空间的比例.设置或检索伸缩盒对象的子元素如何分配其剩余空间.(伸缩盒最老版本) htm ...

  9. 51NOD 1185 威佐夫游戏 V2(威佐夫博弈)

    1185 威佐夫游戏 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取 ...

  10. 并发工具箱 concurrent包的原理分析以及使用

    1.java.util.concurrent 包下的类分类图 locks部分:显式锁(互斥锁和速写锁)相关: atomic部分:原子变量类相关,是构建非阻塞算法的基础: executor部分:线程池相 ...